From a9f29b74d93504631b6dda9a74842eb3a388b27e Mon Sep 17 00:00:00 2001 From: John Senneker Date: Mon, 2 Mar 2020 11:04:57 -0500 Subject: [PATCH] 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. --- tinyxml2.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 2a122bb..faade3b 100755 --- a/tinyxml2.cpp +++ b/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; - } - TIXMLASSERT( filelength >= 0 ); - if ( filelength >= static_cast(-1) ) { + 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(fileLengthSigned); + } + + const size_t maxSizeT = static_cast(-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(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(filelength); TIXMLASSERT( _charBuffer == 0 ); _charBuffer = new char[size+1]; const size_t read = fread( _charBuffer, 1, size, fp );