Skip to content

Commit 583afe4

Browse files
Add a calloc self-test
Add a very basic test of calloc to the selftest program. The selftest program acts in its capacity as a platform compatibility checker rather than in its capacity as a test of the library. The main objective is to report whether calloc returns NULL for a size of 0. Also observe whether a free/alloc sequence returns the address that was just freed and whether a size overflow is properly detected.
1 parent c893235 commit 583afe4

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

programs/test/selftest.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
#else
6666
#include <stdio.h>
6767
#include <stdlib.h>
68+
#define mbedtls_calloc calloc
69+
#define mbedtls_free free
6870
#define mbedtls_printf printf
6971
#define mbedtls_snprintf snprintf
7072
#define mbedtls_exit exit
@@ -77,6 +79,86 @@
7779
#endif
7880

7981

82+
#if defined MBEDTLS_SELF_TEST
83+
/* Sanity check for malloc. This is not expected to fail, and is rather
84+
* intended to display potentially useful information about the platform,
85+
* in particular the behavior of malloc(0). */
86+
static int calloc_self_test( int verbose )
87+
{
88+
int failures = 0;
89+
void *empty1 = mbedtls_calloc( 0, 1 );
90+
void *empty2 = mbedtls_calloc( 0, 1 );
91+
void *buffer1 = mbedtls_calloc( 1, 1 );
92+
void *buffer2 = mbedtls_calloc( 1, 1 );
93+
uintptr_t old_buffer1;
94+
95+
if( empty1 == NULL && empty2 == NULL )
96+
{
97+
if( verbose )
98+
mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
99+
}
100+
else if( empty1 == NULL || empty2 == NULL )
101+
{
102+
if( verbose )
103+
mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
104+
++failures;
105+
}
106+
else if( empty1 == empty2 )
107+
{
108+
if( verbose )
109+
mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
110+
}
111+
else
112+
{
113+
if( verbose )
114+
mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
115+
}
116+
117+
if( buffer1 == NULL || buffer2 == NULL )
118+
{
119+
if( verbose )
120+
mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
121+
++failures;
122+
}
123+
else if( buffer1 == buffer2 )
124+
{
125+
if( verbose )
126+
mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
127+
++failures;
128+
}
129+
else
130+
{
131+
if( verbose )
132+
mbedtls_printf( " CALLOC(1): passed\n" );
133+
}
134+
135+
old_buffer1 = (uintptr_t) buffer1;
136+
mbedtls_free( buffer1 );
137+
buffer1 = mbedtls_calloc( 1, 1 );
138+
if( buffer1 == NULL )
139+
{
140+
if( verbose )
141+
mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
142+
++failures;
143+
}
144+
else
145+
{
146+
if( verbose )
147+
mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
148+
(uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
149+
"same" : "different" );
150+
}
151+
152+
if( verbose )
153+
mbedtls_printf( "\n" );
154+
mbedtls_free( empty1 );
155+
mbedtls_free( empty2 );
156+
mbedtls_free( buffer1 );
157+
mbedtls_free( buffer2 );
158+
return( failures );
159+
}
160+
#endif /* MBEDTLS_SELF_TEST */
161+
80162
static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
81163
{
82164
int ret;
@@ -173,6 +255,7 @@ typedef struct
173255

174256
const selftest_t selftests[] =
175257
{
258+
{"calloc", calloc_self_test},
176259
#if defined(MBEDTLS_MD2_C)
177260
{"md2", mbedtls_md2_self_test},
178261
#endif

0 commit comments

Comments
 (0)