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

fix the safe function behavior

Lee Thomason (grinliz) 14 лет назад
Родитель
Сommit
598c13efa8
2 измененных файлов с 19 добавлено и 16 удалено
  1. 3 4
      tinyxml2.cpp
  2. 16 12
      tinyxml2.h

+ 3 - 4
tinyxml2.cpp

@@ -24,7 +24,6 @@ distribution.
 #include "tinyxml2.h"
 
 #if 1
-	#include <cstdarg>
 	#include <cstdio>
 	#include <cstdlib>
 	#include <new>
@@ -998,7 +997,7 @@ void XMLAttribute::SetAttribute( const char* v )
 void XMLAttribute::SetAttribute( int v )
 {
 	char buf[BUF_SIZE];
-	TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );	
+	TIXML_SNPRINTF( buf, BUF_SIZE, "%d", v );	
 	value.SetStr( buf );
 }
 
@@ -1534,10 +1533,10 @@ void XMLPrinter::Print( const char* format, ... )
 			int len = -1;
 			int expand = 1000;
 			while ( len < 0 ) {
-				len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
+				len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), _TRUNCATE, format, va );
 				if ( len < 0 ) {
-					accumulator.PushArr( expand );
 					expand *= 3/2;
+					accumulator.PushArr( expand );
 				}
 			}
 			char* p = buffer.PushArr( len ) - 1;

+ 16 - 12
tinyxml2.h

@@ -29,6 +29,7 @@ distribution.
 	#include <climits>
 	#include <cstdio>
 	#include <cstring>
+	#include <cstdarg>
 #else
 	// Not completely sure all the interesting systems
 	// can handle the new headers; can switch this if
@@ -70,27 +71,30 @@ distribution.
 #endif
 
 
-// Deprecated library function hell. Compilers want to use the
-// new safe versions. This probably doesn't fully address the problem,
-// but it gets closer. There are too many compilers for me to fully
-// test. If you get compilation troubles, undefine TIXML_SAFE
-
 #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
 	// Microsoft visual studio, version 2005 and higher.
-	#define TIXML_SNPRINTF _snprintf_s
+	/*int _snprintf_s(
+	   char *buffer,
+	   size_t sizeOfBuffer,
+	   size_t count,	
+	   const char *format [,
+		  argument] ... 
+	);*/
+	inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) {
+	    va_list va;
+		va_start( va, format );
+		int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
+	    va_end( va );
+		return result;
+	}
 	#define TIXML_SSCANF   sscanf_s
-#elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
-	// Microsoft visual studio, version 6 and higher.
-	//#pragma message( "Using _sn* functions." )
-	#define TIXML_SNPRINTF _snprintf
-	#define TIXML_SSCANF   sscanf
 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
 	// GCC version 3 and higher
 	//#warning( "Using sn* functions." )
 	#define TIXML_SNPRINTF snprintf
 	#define TIXML_SSCANF   sscanf
 #else
-	#define TIXML_SNPRINTF snprintf
+	#define TIXML_SNPRINTF snprintf( buf, size, x ) snprintf( buf, size, x )
 	#define TIXML_SSCANF   sscanf
 #endif