Jelajahi Sumber

Make code more defensive
* initialize out-parameter for cases the callee fails to
* add assert for length being within range
* replace post-assert with pre-assert

Also replace the loop with an equivalent memcpy() call

Dmitry-Me 11 tahun lalu
induk
melakukan
63f3de1a3c
1 mengubah file dengan 7 tambahan dan 6 penghapusan
  1. 7 6
      tinyxml2.cpp

+ 7 - 6
tinyxml2.cpp

@@ -216,13 +216,14 @@ const char* StrPair::GetStr()
                     //   中 or 中
 
                     if ( *(p+1) == '#' ) {
-                        char buf[10] = { 0 };
-                        int len;
+                        const int buflen = 10;
+                        char buf[buflen] = { 0 };
+                        int len = 0;
                         p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
-                        for( int i=0; i<len; ++i ) {
-                            *q++ = buf[i];
-                        }
-                        TIXMLASSERT( q <= p );
+                        TIXMLASSERT( 0 <= len && len <= buflen );
+                        TIXMLASSERT( q + len <= p );
+                        memcpy( q, buf, len );
+                        q += len;
                     }
                     else {
                         int i=0;