From 934c1b79527b5ac2f178adb8a553c7d173f121df Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Wed, 6 Mar 2019 14:55:51 -0600 Subject: [PATCH 1/3] Use snprintf with Win build --- wolfcrypt/src/logging.c | 5 +++++ wolfssl/wolfcrypt/types.h | 16 ++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 8f83bd5a3..53633b0fd 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -124,6 +124,11 @@ 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 e29f8fcbd..afe97b575 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -382,17 +382,13 @@ /* snprintf is used in asn.c for GetTimeString, PKCS7 test, and when debugging is turned on */ - #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 */ - #include - #endif - #define XSNPRINTF snprintf - #else - #define XSNPRINTF _snprintf + #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 */ + #include #endif + #define XSNPRINTF snprintf #if defined(WOLFSSL_CERT_EXT) || defined(HAVE_ALPN) /* use only Thread Safe version of strtok */ From be83a54f223b8b8502407f2338803902ea0800cd Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Thu, 7 Mar 2019 12:07:00 -0600 Subject: [PATCH 2/3] Handle older MSC versions --- wolfssl/wolfcrypt/types.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index afe97b575..cf27180de 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -382,13 +382,27 @@ /* snprintf is used in asn.c for GetTimeString, PKCS7 test, and when debugging is turned on */ - #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 */ - #include + #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 */ + #include + #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 - #define XSNPRINTF snprintf #if defined(WOLFSSL_CERT_EXT) || defined(HAVE_ALPN) /* use only Thread Safe version of strtok */ From d26a6b59a3ba5ea6a59cd915607d13581243ccdb Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Fri, 8 Mar 2019 10:55:34 -0600 Subject: [PATCH 3/3] Wrapper for MSC < VS2015 --- wolfcrypt/src/logging.c | 5 ----- wolfssl/wolfcrypt/types.h | 41 +++++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 17 deletions(-) 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 */