diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 53633b0fd..8f83bd5a3 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -124,11 +124,6 @@ static int loggingEnabled = 0; static struct log mynewt_log; #endif /* WOLFSSL_APACHE_MYNEWT */ -#ifdef _MSC_VER -/* 4996 warning to use MS extensions e.g., sprintf_s instead of XSPRINTF */ -#pragma warning(disable: 4996) -#endif /* _MSC_VER */ - #endif /* DEBUG_WOLFSSL */ diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index cf27180de..71574286d 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -391,18 +391,35 @@ #endif #define XSNPRINTF snprintf #else - #if defined(_MSC_VER) && (_MSC_VER >= 1900) - /* Beginning with the UCRT in Visual Studio 2015 and - Windows 10, snprintf is no longer identical to - _snprintf. The snprintf function behavior is now - C99 standard compliant. */ - #define XSNPRINTF snprintf - #else - /* _snprintf only null terminates the string if return len - is less than count */ - #define XSNPRINTF _snprintf - #endif - #endif + #ifdef _MSC_VER + #if (_MSC_VER >= 1900) + /* Beginning with the UCRT in Visual Studio 2015 and + Windows 10, snprintf is no longer identical to + _snprintf. The snprintf function behavior is now + C99 standard compliant. */ + #define XSNPRINTF snprintf + #else + /* 4996 warning to use MS extensions e.g., _sprintf_s + instead of _snprintf */ + #pragma warning(disable: 4996) + static WC_INLINE + int xsnprintf(char *buffer, size_t bufsize, + const char *format, ...) { + va_list ap; + int ret; + + if ((int)bufsize <= 0) return -1; + va_start(ap, format); + ret = vsnprintf(buffer, bufsize, format, ap); + if (ret >= (int)bufsize) + ret = -1; + va_end(ap); + return ret; + } + #define XSNPRINTF xsnprintf + #endif /* (_MSC_VER >= 1900) */ + #endif /* _MSC_VER */ + #endif /* USE_WINDOWS_API */ #if defined(WOLFSSL_CERT_EXT) || defined(HAVE_ALPN) /* use only Thread Safe version of strtok */