diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 1a421ec1a..d828003c3 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -398,14 +398,41 @@ #ifndef USE_WINDOWS_API #if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \ defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM) - /* case where stdio is not included else where but is needed for - * snprintf */ + /* case where stdio is not included else where but is needed + for snprintf */ #include #endif #define XSNPRINTF snprintf #else - #define XSNPRINTF _snprintf - #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 */