From d14acbc31a53e4ed06140c38c55399bcd88bd59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 29 May 2015 11:26:37 +0200 Subject: [PATCH] Test assumptions we make about the platform Things that are not guaranteed by the standard but should be true of all platforms of interest to us: - 8-bit chars - NULL pointers represented by all-bits-zero --- include/mbedtls/check_config.h | 9 +++++++++ programs/test/selftest.c | 13 +++++++++++++ tests/suites/main_test.function | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index a67978068..5f620be0d 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -30,6 +30,15 @@ #ifndef MBEDTLS_CHECK_CONFIG_H #define MBEDTLS_CHECK_CONFIG_H +/* + * We assume CHAR_BIT is 8 in many places. In practice, this is true on our + * target platforms, so not an issue, but let's just be extra sure. + */ +#include +#if CHAR_BIT != 8 +#error "mbed TLS requires a platform with 8-bit chars" +#endif + #if defined(MBEDTLS_DEPRECATED_WARNING) && \ !defined(__GNUC__) && !defined(__clang__) #error "MBEDTLS_DEPRECATED_WARNING only works with GCC and Clang" diff --git a/programs/test/selftest.c b/programs/test/selftest.c index 902225e33..5c1d354a8 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -72,6 +72,19 @@ int main( int argc, char *argv[] ) #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) unsigned char buf[1000000]; #endif + void *pointer; + + /* + * The C standard doesn't guarantee that all-bits-0 is the representation + * of a NULL pointer. We do however use that in our code for initializing + * structures, which should work on every modern platform. Let's be sure. + */ + memset( &pointer, 0, sizeof( void * ) ); + if( pointer != NULL ) + { + mbedtls_printf( "all-bits-zero is not a NULL pointer\n" ); + return( 1 ); + } if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 ) v = 0; diff --git a/tests/suites/main_test.function b/tests/suites/main_test.function index ecd51824f..f1ef9175c 100644 --- a/tests/suites/main_test.function +++ b/tests/suites/main_test.function @@ -216,6 +216,7 @@ int main() FILE *file; char buf[5000]; char *params[50]; + void *pointer; #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \ !defined(TEST_SUITE_MEMORY_BUFFER_ALLOC) @@ -223,6 +224,18 @@ int main() mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) ); #endif + /* + * The C standard doesn't guarantee that all-bits-0 is the representation + * of a NULL pointer. We do however use that in our code for initializing + * structures, which should work on every modern platform. Let's be sure. + */ + memset( &pointer, 0, sizeof( void * ) ); + if( pointer != NULL ) + { + mbedtls_fprintf( stderr, "all-bits-zero is not a NULL pointer\n" ); + return( 1 ); + } + file = fopen( filename, "r" ); if( file == NULL ) {