Add an mbedtls_calloc(SIZE_MAX/2, SIZE_MAX/2) test

It should return NULL and not a valid pointer.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2023-05-06 08:52:50 -04:00
parent 84356a16e9
commit aae3208c29
2 changed files with 15 additions and 3 deletions

View file

@ -3708,7 +3708,8 @@
*
* Defining MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_STD_CALLOC at the same time is not possible.
* MBEDTLS_PLATFORM_CALLOC_MACRO and MBEDTLS_PLATFORM_FREE_MACRO must both be defined or undefined at the same time.
* MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE do not have to be defined at the same time, as, if they are used, dynamic setup of these functions is possible. See the tree above to see how are they handled in all cases.
* MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE do not have to be defined at the same time, as, if they are used,
* dynamic setup of these functions is possible. See the tree above to see how are they handled in all cases.
*/
/** \def MBEDTLS_PLATFORM_STD_CALLOC
*
@ -3722,7 +3723,7 @@
//#define MBEDTLS_PLATFORM_STD_CALLOC calloc
/** \def MBEDTLS_PLATFORM_STD_FREE
*
* Default free to use, can be undefined.
* Default free to use, can be undefined. See the description above for more details (same principles as for MBEDTLS_PLATFORM_STD_CALLOC apply).
* NULL is a valid parameter, and the function must do nothing.
* A non-null parameter will always be a pointer previously returned by #MBEDTLS_PLATFORM_STD_CALLOC and not yet freed.
*/

View file

@ -77,7 +77,10 @@ static int calloc_self_test(int verbose)
unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Walloc-size-larger-than="
unsigned char *buffer5 = mbedtls_calloc(SIZE_MAX/2, SIZE_MAX/2);
#pragma GCC diagnostic pop
if (empty1 == NULL && empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
@ -172,6 +175,13 @@ static int calloc_self_test(int verbose)
}
}
if (buffer5 != NULL) {
++failures;
if (verbose) {
mbedtls_printf(" CALLOC(SIZE_MAX/2, SIZE_MAX/2): failed (returned a valid pointer)\n");
}
}
if (verbose) {
mbedtls_printf("\n");
}
@ -181,6 +191,7 @@ static int calloc_self_test(int verbose)
mbedtls_free(buffer2);
mbedtls_free(buffer3);
mbedtls_free(buffer4);
mbedtls_free(buffer5);
return failures;
}
#endif /* MBEDTLS_SELF_TEST */