0ad/source/lib/sysdep/unix/printf.cpp
Ykkrosh a265a441fd # Fixed string handling for Windows/Linux compatibility.
* vsnprintf2: Made compatible between GCC and MSVC - it now always
null-terminates the buffer, and returns -1 on overflow. Fixes #158.
Added tests.
 * MeshManager: Use shared_ptr.expired() instead of checking for
bad_weak_ptr exception.
 * Xeromyces: Added tests for GetXMBPath, because it does unusual things
in sscanf which MSVC's /analyze complains about.
 * ConfigDB, ScriptGlue: Replaced some asserts with return-on-failure,
to avoid invalid array accesses when continuing after the assert (as
complained about by /analyze).
 * CStr: Removed "using namespace std". Added tests for handling of
invalid UTF-8.

This was SVN commit r4625.
2006-11-07 21:03:13 +00:00

26 lines
688 B
C++

#include "precompiled.h"
#include <cstdio>
#include <cstdarg>
// See declaration in sysdep.h for explanation of need
int vsnprintf2(char* buffer, size_t count, const char* format, va_list argptr)
{
int ret = vsnprintf(buffer, count, format, argptr);
/*
"The glibc implementation of the functions snprintf() and vsnprintf() conforms
to the C99 standard ... since glibc version 2.1. Until glibc 2.0.6 they would
return -1 when the output was truncated."
- man printf
MSVC's _vsnprintf still returns -1, so we want this one to do the same (for
compatibility), if the output (including the terminating null) is truncated.
*/
if (ret >= (int)count)
return -1;
return ret;
}