diff --git a/library/debug.c b/library/debug.c index 672e04537..2a559b54b 100644 --- a/library/debug.c +++ b/library/debug.c @@ -65,7 +65,16 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, va_start( argp, format ); #if defined(_WIN32) +#if defined(_TRUNCATE) ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); +#else + ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); + if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) + { + str[DEBUG_BUF_SIZE-1] = '\0'; + ret = -1; + } +#endif #else ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); #endif diff --git a/library/platform.c b/library/platform.c index 0606214b3..3161a4c00 100644 --- a/library/platform.c +++ b/library/platform.c @@ -75,7 +75,16 @@ int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) return( -1 ); va_start( argp, fmt ); +#if defined(_TRUNCATE) ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); +#else + ret = _vsnprintf( s, n, fmt, argp ); + if( ret < 0 || (size_t) ret == n ) + { + s[n-1] = '\0'; + ret = -1; + } +#endif va_end( argp ); return( ret );