Fix snprintf test

Our Windows implementation based on vsnprintf_s( ..., _TRUNCATE ) sometimes
writes *two* terminating NULLs. Allow for that, but obviously bytes past the
end of the buffer mustn't be touched.
This commit is contained in:
Manuel Pégourié-Gonnard 2015-06-26 11:24:32 +02:00
parent 9db2887672
commit 4b00f08e20
2 changed files with 18 additions and 14 deletions

View File

@ -71,13 +71,15 @@ static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
{
int ret;
char buf[10] = "xxxxxxxxx";
const char ref[10] = "xxxxxxxxx";
ret = mbedtls_snprintf( buf, n, "%s", "123" );
if( ret < 0 || (size_t) ret >= n )
ret = -1;
if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
ref_ret != ret )
if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
ref_ret != ret ||
memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
{
return( 1 );
}
@ -88,11 +90,11 @@ static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
static int run_test_snprintf( void )
{
return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
test_snprintf( 4, "123\0xxxxx", 3 ) != 0 ||
test_snprintf( 5, "123\0xxxxx", 3 ) != 0 );
test_snprintf( 1, "", -1 ) != 0 ||
test_snprintf( 2, "1", -1 ) != 0 ||
test_snprintf( 3, "12", -1 ) != 0 ||
test_snprintf( 4, "123", 3 ) != 0 ||
test_snprintf( 5, "123", 3 ) != 0 );
}
int main( int argc, char *argv[] )

View File

@ -214,13 +214,15 @@ static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
{
int ret;
char buf[10] = "xxxxxxxxx";
const char ref[10] = "xxxxxxxxx";
ret = mbedtls_snprintf( buf, n, "%s", "123" );
if( ret < 0 || (size_t) ret >= n )
ret = -1;
if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
ref_ret != ret )
if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
ref_ret != ret ||
memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
{
return( 1 );
}
@ -231,11 +233,11 @@ static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
static int run_test_snprintf( void )
{
return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
test_snprintf( 4, "123\0xxxxx", 3 ) != 0 ||
test_snprintf( 5, "123\0xxxxx", 3 ) != 0 );
test_snprintf( 1, "", -1 ) != 0 ||
test_snprintf( 2, "1", -1 ) != 0 ||
test_snprintf( 3, "12", -1 ) != 0 ||
test_snprintf( 4, "123", 3 ) != 0 ||
test_snprintf( 5, "123", 3 ) != 0 );
}
int main()