fix the safe function behavior

This commit is contained in:
Lee Thomason (grinliz) 2012-04-06 21:18:23 -07:00
parent f14695f26b
commit 598c13efa8
2 changed files with 19 additions and 16 deletions

View File

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

View File

@ -29,6 +29,7 @@ distribution.
#include <climits> #include <climits>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <cstdarg>
#else #else
// Not completely sure all the interesting systems // Not completely sure all the interesting systems
// can handle the new headers; can switch this if // can handle the new headers; can switch this if
@ -70,27 +71,30 @@ distribution.
#endif #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 ) #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
// Microsoft visual studio, version 2005 and higher. // 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 #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 ) #elif defined(__GNUC__) && (__GNUC__ >= 3 )
// GCC version 3 and higher // GCC version 3 and higher
//#warning( "Using sn* functions." ) //#warning( "Using sn* functions." )
#define TIXML_SNPRINTF snprintf #define TIXML_SNPRINTF snprintf
#define TIXML_SSCANF sscanf #define TIXML_SSCANF sscanf
#else #else
#define TIXML_SNPRINTF snprintf #define TIXML_SNPRINTF snprintf( buf, size, x ) snprintf( buf, size, x )
#define TIXML_SSCANF sscanf #define TIXML_SSCANF sscanf
#endif #endif