Rationalize other snprintf() uses

parent 16853684
......@@ -34,16 +34,6 @@
#include <stdio.h>
#include <string.h>
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
#if !defined snprintf
#define snprintf _snprintf
#endif
#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif
#endif /* _MSC_VER */
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
......@@ -67,13 +57,15 @@ char *mbedtls_debug_fmt( const char *format, ... )
{
va_list argp;
static char str[512];
int maxlen = sizeof( str ) - 1;
va_start( argp, format );
vsnprintf( str, maxlen, format, argp );
#if defined(_WIN32)
_vsnprintf_s( str, sizeof( str ), _TRUNCATE, format, argp );
#else
vsnprintf( str, sizeof( str ), format, argp );
#endif
va_end( argp );
str[maxlen] = '\0';
return( str );
}
......@@ -81,7 +73,6 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
const char *file, int line, const char *text )
{
char str[512];
int maxlen = sizeof( str ) - 1;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return;
......@@ -92,8 +83,7 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
return;
}
mbedtls_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
str[maxlen] = '\0';
mbedtls_snprintf( str, sizeof( str ), "%s(%04d): %s\n", file, line, text );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
......@@ -102,7 +92,6 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
const char *text, int ret )
{
char str[512];
int maxlen = sizeof( str ) - 1;
size_t idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
......@@ -117,12 +106,11 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
return;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s() returned %d (-0x%04x)\n",
text, ret, -ret );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
......@@ -132,18 +120,17 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
{
char str[512];
char txt[17];
size_t i, maxlen = sizeof( str ) - 1, idx = 0;
size_t i, idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
text, (unsigned int) len );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
......@@ -157,7 +144,7 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
{
if( i > 0 )
{
mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
......@@ -165,14 +152,14 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
}
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
idx += mbedtls_snprintf( str + idx, maxlen - idx, "%04x: ",
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
(unsigned int) i );
}
idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x",
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
(unsigned int) buf[i] );
txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
}
......@@ -180,9 +167,9 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
if( len > 0 )
{
for( /* i = i */; i % 16 != 0; i++ )
idx += mbedtls_snprintf( str + idx, maxlen - idx, " " );
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
mbedtls_snprintf( str + idx, maxlen - idx, " %s\n", txt );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
}
......@@ -193,17 +180,14 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
const char *text, const mbedtls_ecp_point *X )
{
char str[512];
int maxlen = sizeof( str ) - 1;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
return;
mbedtls_snprintf( str, maxlen, "%s(X)", text );
str[maxlen] = '\0';
mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
mbedtls_snprintf( str, maxlen, "%s(Y)", text );
str[maxlen] = '\0';
mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
}
#endif /* MBEDTLS_ECP_C */
......@@ -214,7 +198,7 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
const char *text, const mbedtls_mpi *X )
{
char str[512];
int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
int j, k, zeros = 1;
size_t i, n, idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
......@@ -229,12 +213,11 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
break;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
......@@ -254,16 +237,16 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
{
if( j > 0 )
{
mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
idx = 0;
}
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
}
idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
j++;
......@@ -275,13 +258,13 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
{
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
{
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
}
idx += mbedtls_snprintf( str + idx, maxlen - idx, " 00" );
idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
}
mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
}
#endif /* MBEDTLS_BIGNUM_C */
......@@ -328,33 +311,29 @@ void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
const char *text, const mbedtls_x509_crt *crt )
{
char str[1024], prefix[64];
int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
int i = 0, idx = 0;
if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
return;
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
{
mbedtls_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
prefix[maxlen] = '\0';
mbedtls_snprintf( prefix, sizeof( prefix ), "%s(%04d): ", file, line );
}
else
prefix[0] = '\0';
maxlen = sizeof( str ) - 1;
while( crt != NULL )
{
char buf[1024];
mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
idx = mbedtls_snprintf( str, sizeof( str ), "%s(%04d): ", file, line );
mbedtls_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%s #%d:\n%s",
text, ++i, buf );
str[maxlen] = '\0';
ssl->conf->f_dbg( ssl->conf->p_dbg, level, str );
debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
......
......@@ -149,10 +149,6 @@
#include "mbedtls/xtea.h"
#endif
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#define snprintf _snprintf
#endif
void mbedtls_strerror( int ret, char *buf, size_t buflen )
{
......@@ -163,8 +159,6 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
return;
memset( buf, 0x00, buflen );
/* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
buflen -= 1;
if( ret < 0 )
ret = -ret;
......@@ -474,6 +468,8 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
mbedtls_snprintf( buf, buflen, "X509 - Allocation of memory failed" );
if( use_ret == -(MBEDTLS_ERR_X509_FILE_IO_ERROR) )
mbedtls_snprintf( buf, buflen, "X509 - Read/write of file failed" );
if( use_ret == -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL) )
mbedtls_snprintf( buf, buflen, "X509 - Destination buffer is too small" );
#endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
// END generated code
......
......@@ -77,11 +77,6 @@ static int wsa_init_done = 0;
#include <stdlib.h>
#include <stdio.h>
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#define snprintf _snprintf
#endif
#include <time.h>
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
......
......@@ -598,55 +598,14 @@ FN_OID_TYPED_FROM_ASN1(oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, oid_pkcs12_pbe_alg)
FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pbe_alg, mbedtls_md_type_t, md_alg, mbedtls_cipher_type_t, cipher_alg)
#endif /* MBEDTLS_PKCS12_C */
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#include <stdarg.h>
#if !defined vsnprintf
#define vsnprintf _vsnprintf
#endif // vsnprintf
/*
* Windows _snprintf and _vsnprintf are not compatible to linux versions.
* Result value is not size of buffer needed, but -1 if no fit is possible.
*
* This fuction tries to 'fix' this by at least suggesting enlarging the
* size by 20.
*/
static int compat_snprintf( char *str, size_t size, const char *format, ... )
{
va_list ap;
int res = -1;
va_start( ap, format );
res = vsnprintf( str, size, format, ap );
va_end( ap );
// No quick fix possible
if( res < 0 )
return( (int) size + 20 );
return( res );
}
#define snprintf compat_snprintf
#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
#define SAFE_SNPRINTF() \
{ \
if( ret == -1 ) \
return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
\
if( (unsigned int) ret >= n ) { \
p[n - 1] = '\0'; \
return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
} \
\
n -= (unsigned int) ret; \
p += (unsigned int) ret; \
}
#define OID_SAFE_SNPRINTF \
do { \
if( ret < 0 || (size_t) ret >= n ) \
return( MBEDTLS_ERR_OID_BUF_TOO_SMALL ); \
\
n -= (size_t) ret; \
p += (size_t) ret; \
} while( 0 )
/* Return the x.y.z.... style numeric string for the given OID */
int mbedtls_oid_get_numeric_string( char *buf, size_t size,
......@@ -664,7 +623,7 @@ int mbedtls_oid_get_numeric_string( char *buf, size_t size,
if( oid->len > 0 )
{
ret = mbedtls_snprintf( p, n, "%d.%d", oid->p[0] / 40, oid->p[0] % 40 );
SAFE_SNPRINTF();
OID_SAFE_SNPRINTF;
}
value = 0;
......@@ -681,7 +640,7 @@ int mbedtls_oid_get_numeric_string( char *buf, size_t size,
{
/* Last byte */
ret = mbedtls_snprintf( p, n, ".%d", value );
SAFE_SNPRINTF();
OID_SAFE_SNPRINTF;
value = 0;
}
}
......
......@@ -42,10 +42,6 @@
#include <stdio.h>
HEADER_INCLUDED
#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
!defined(EFI32)
#define snprintf _snprintf
#endif
void mbedtls_strerror( int ret, char *buf, size_t buflen )
{
......@@ -56,8 +52,6 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen )
return;
memset( buf, 0x00, buflen );
/* Reduce buflen to make sure MSVC _snprintf() ends with \0 as well */
buflen -= 1;
if( ret < 0 )
ret = -ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment