Rework the calloc buffer initialization test in selftest.c

This way it's more in line with development.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2023-07-14 09:53:08 -04:00
parent dc11cd1668
commit 5ffea9dc77

View File

@ -77,23 +77,49 @@ static int calloc_self_test(int verbose)
void *empty2 = mbedtls_calloc(0, 1);
void *buffer1 = mbedtls_calloc(1, 1);
void *buffer2 = mbedtls_calloc(1, 1);
unsigned int buf_size = 256;
unsigned char *buffer3 = mbedtls_calloc(buf_size, sizeof(unsigned char));
if (empty1 == NULL && empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(0): passed (NULL)\n");
mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
}
} else if (empty1 == NULL || empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(0): failed (mix of NULL and non-NULL)\n");
mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
}
++failures;
} else if (empty1 == empty2) {
if (verbose) {
mbedtls_printf(" CALLOC(0): passed (same non-null)\n");
mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
}
} else {
if (verbose) {
mbedtls_printf(" CALLOC(0): passed (distinct non-null)\n");
mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
}
}
mbedtls_free(empty1);
mbedtls_free(empty2);
empty1 = mbedtls_calloc(1, 0);
empty2 = mbedtls_calloc(1, 0);
if (empty1 == NULL && empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
}
} else if (empty1 == NULL || empty2 == NULL) {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
}
++failures;
} else if (empty1 == empty2) {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
}
} else {
if (verbose) {
mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
}
}
@ -126,6 +152,16 @@ static int calloc_self_test(int verbose)
}
}
for (unsigned int i = 0; i < buf_size; i++) {
if (buffer3[i] != 0) {
++failures;
if (verbose) {
mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n", buf_size);
}
break;
}
}
if (verbose) {
mbedtls_printf("\n");
}
@ -133,6 +169,7 @@ static int calloc_self_test(int verbose)
mbedtls_free(empty2);
mbedtls_free(buffer1);
mbedtls_free(buffer2);
mbedtls_free(buffer3);
return failures;
}
#endif /* MBEDTLS_SELF_TEST */
@ -167,23 +204,6 @@ static int run_test_snprintf(void)
test_snprintf(5, "123", 3) != 0;
}
static int run_test_mbedtls_calloc(void)
{
unsigned int buf_size = 256;
unsigned char *buf;
int ret = -1;
buf = mbedtls_calloc(buf_size, sizeof(unsigned char));
for (unsigned int i = 0; i < buf_size; i++) {
if (buf[i] != 0) {
ret = -1;
goto exit;
}
}
ret = 0;
exit:
mbedtls_free(buf);
return ret;
}
/*
* Check if a seed file is present, and if not create one for the entropy
* self-test. If this fails, we attempt the test anyway, so no error is passed
@ -393,12 +413,6 @@ int main(int argc, char *argv[])
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
}
/* Make sure that mbedtls_calloc zeroizes the buffer */
if (run_test_mbedtls_calloc() != 0) {
mbedtls_printf("the calloc implementation does not zeroize the buffer\n");
mbedtls_exit(MBEDTLS_EXIT_FAILURE);
}
for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
if (strcmp(*argp, "--quiet") == 0 ||
strcmp(*argp, "-q") == 0) {