Fixes #663: added safe snprintf code by Wolf-Dieter

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5306 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 6f2b0634fbb9a2e5a113a997e6fdbee48ce68aa4
Former-commit-id: 1b8ecdf3aa769c301a46cc72e19f092926f5d366
This commit is contained in:
kmetykog 2013-03-12 07:22:45 +00:00
parent 80d8e4c45c
commit 0d867d84a3
2 changed files with 34 additions and 2 deletions

View file

@ -63,10 +63,37 @@ char *strtok_r(char *str, const char *delim, char **nextp)
if (*str)
*str++ = '\0';
*nextp = str;
return ret;
}
#endif // HAVE_STRTOK_R
// Ticket #663 - MSVC implementation of snprintf is not safe
// We provide our own version of the function,
// that ensures 0 ending for the string.
//
// See http://msdn.microsoft.com/en-us/library/d3xd30zz(v=vs.80).aspx
//
// "Each of these functions takes a pointer to an argument list,
// then formats and writes up to count characters of the given data
// to the memory pointed to by buffer and appends a terminating null.
// If count is _TRUNCATE, then these functions write as much of the
// string as will fit in buffer while leaving room for a terminating
// null. If the entire string (with terminating null) fits in buffer,
// then these functions return the number of characters written
// (not including the terminating null); otherwise, these functions
// return -1 to indicate that truncation occurred."
#ifdef _MSC_VER
int SD_snprintf(char *str, size_t size, const char *format, ...)
{
va_list vaArgs;
va_start(vaArgs, format);
int len = _vsnprintf_s(str, size, _TRUNCATE, format, vaArgs);
va_end(vaArgs);
if (size > 0)
str[size - 1] = 0;
return len;
}
#endif // _MSC_VER

View file

@ -98,7 +98,12 @@ PORTABILITY_API char *strtok_r(char *str, const char *delim, char **nextp);
#define isnan _isnan
#define snprintf _snprintf
// Ticket #663 - MSVC implementation of snprintf is not safe
// We provide our own version of the function,
// that ensures 0 ending for the string.
#include <cstdarg>
#include <cstdio>
#define snprintf SD_snprintf
#define access _access