Просмотр исходного кода

Fixed warning caused by sloppy conversion between signed/unsigned types.

Also, the comparison between size_t max and the actual file size is done
as an unsigned long long, since that type is guaranteed to be at least
64 bits, even on a 32-bit architecture.
John Senneker 6 лет назад
Родитель
Сommit
a9f29b74d9
1 измененных файлов с 16 добавлено и 8 удалено
  1. 16 8
      tinyxml2.cpp

+ 16 - 8
tinyxml2.cpp

@@ -2278,15 +2278,23 @@ XMLError XMLDocument::LoadFile( FILE* fp )
     }
 
     TIXML_FSEEK( fp, 0, SEEK_END );
-    const long long filelength = TIXML_FTELL( fp );
-    TIXML_FSEEK( fp, 0, SEEK_SET );
-    if ( filelength == -1L ) {
-        SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
-        return _errorID;
+
+    unsigned long long filelength;
+    {
+        const long long fileLengthSigned = TIXML_FTELL( fp );
+        TIXML_FSEEK( fp, 0, SEEK_SET );
+        if ( fileLengthSigned == -1L ) {
+            SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
+            return _errorID;
+        }
+        TIXMLASSERT( fileLengthSigned >= 0 );
+        filelength = static_cast<unsigned long long>(fileLengthSigned);
     }
-    TIXMLASSERT( filelength >= 0 );
 
-    if ( filelength >= static_cast<size_t>(-1) ) {
+    const size_t maxSizeT = static_cast<size_t>(-1);
+    // We'll do the comparison as an unsigned long long, because that's guaranteed to be at
+    // least 8 bytes, even on a 32-bit platform.
+    if ( filelength >= static_cast<unsigned long long>(maxSizeT) ) {
         // Cannot handle files which won't fit in buffer together with null terminator
         SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
         return _errorID;
@@ -2297,7 +2305,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
         return _errorID;
     }
 
-    const size_t size = filelength;
+    const size_t size = static_cast<size_t>(filelength);
     TIXMLASSERT( _charBuffer == 0 );
     _charBuffer = new char[size+1];
     const size_t read = fread( _charBuffer, 1, size, fp );