From f76cd1b84083b449a853343775def314afdb3664 Mon Sep 17 00:00:00 2001 From: Yasuhiro Fujii Date: Sat, 24 Jun 2017 14:30:19 +0900 Subject: [PATCH] add _snprintf() wrapper. --- windows/JackCompilerDeps_os.h | 24 ++++++++++++++++++++++-- windows/JackSystemDeps_os.h | 5 +---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/windows/JackCompilerDeps_os.h b/windows/JackCompilerDeps_os.h index 643856b2..09812d4f 100644 --- a/windows/JackCompilerDeps_os.h +++ b/windows/JackCompilerDeps_os.h @@ -40,9 +40,29 @@ #endif #if defined(_MSC_VER) /* Added by JE - 31-01-2012 */ -#define vsnprintf _vsnprintf -#define snprintf _snprintf #define strdup _strdup +#if _MSC_VER < 1900 +// This wrapper is not fully standard-compliant. _snprintf() does not +// distinguish whether a result is truncated or a format error occurs. +inline int vsnprintf(char* buf, size_t buf_len, const char* fmt, va_list args) +{ + int str_len = _vsnprintf(buf, buf_len - 1, fmt, args); + if (str_len == buf_len - 1 || str_len < 0) { + buf[buf_len - 1] = '\0'; + return buf_len - 1; + } + return str_len; +} + +inline int snprintf(char* buf, size_t buf_len, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + int str_len = vsnprintf(buf, buf_len, fmt, args); + va_end(args); + return str_len; +} +#endif #endif #endif diff --git a/windows/JackSystemDeps_os.h b/windows/JackSystemDeps_os.h index 07f1bdfb..60718519 100644 --- a/windows/JackSystemDeps_os.h +++ b/windows/JackSystemDeps_os.h @@ -22,6 +22,7 @@ #define __JackSystemDeps_WIN32__ #include +#include "JackCompilerDeps.h" #ifndef PATH_MAX #define PATH_MAX 512 @@ -49,10 +50,6 @@ #define JACK_DEBUG false #endif -#if defined(_MSC_VER) -#define snprintf _snprintf -#endif - inline int setenv(const char* name, const char* value, int overwrite) { if (overwrite == 0 && getenv(name) != NULL) {