Merge pull request #380 from Dmitry-Me/getRidOfWtypeLimits

Get rid of -Wtype-limits warning
This commit is contained in:
Lee Thomason 2015-09-29 19:49:46 -07:00
commit a589da416c
1 changed files with 21 additions and 1 deletions

View File

@ -1914,6 +1914,26 @@ XMLError XMLDocument::LoadFile( const char* filename )
return _errorID; return _errorID;
} }
// This is likely overengineered template art to have a check that unsigned long value incremented
// by one still fits into size_t. If size_t type is larger than unsigned long type
// (x86_64-w64-mingw32 target) then the check is redundant and gcc and clang emit
// -Wtype-limits warning. This piece makes the compiler select code with a check when a check
// is useful and code with no check when a check is redundant depending on how size_t and unsigned long
// types sizes relate to each other.
template
<bool = (sizeof(unsigned long) >= sizeof(size_t))>
struct LongFitsIntoSizeTMinusOne {
static bool Fits( unsigned long value )
{
return value < (size_t)-1;
}
};
template <>
bool LongFitsIntoSizeTMinusOne<false>::Fits( unsigned long /*value*/ )
{
return true;
}
XMLError XMLDocument::LoadFile( FILE* fp ) XMLError XMLDocument::LoadFile( FILE* fp )
{ {
@ -1933,7 +1953,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
return _errorID; return _errorID;
} }
if ( (unsigned long)filelength >= (size_t)-1 ) { if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {
// Cannot handle files which won't fit in buffer together with null terminator // Cannot handle files which won't fit in buffer together with null terminator
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID; return _errorID;