First check, then convert
diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index 03ce1b9..6e1985b 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -1852,12 +1852,12 @@
         return _errorID;

     }

 

-    const size_t size = filelength;

-    if ( size == 0 ) {

+    if ( filelength == 0 ) {

         SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );

         return _errorID;

     }

 

+    const size_t size = filelength;

     _charBuffer = new char[size+1];

     size_t read = fread( _charBuffer, 1, size, fp );

     if ( read != size ) {