From 598c13efa86e1d81198ce49f50904b90b54b1d39 Mon Sep 17 00:00:00 2001 From: "Lee Thomason (grinliz)" Date: Fri, 6 Apr 2012 21:18:23 -0700 Subject: [PATCH] fix the safe function behavior --- tinyxml2.cpp | 7 +++---- tinyxml2.h | 28 ++++++++++++++++------------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 9e92ae2..72c2dac 100644 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -24,7 +24,6 @@ distribution. #include "tinyxml2.h" #if 1 - #include #include #include #include @@ -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; diff --git a/tinyxml2.h b/tinyxml2.h index bf00964..f9348ab 100644 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -29,6 +29,7 @@ distribution. #include #include #include + #include #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