Skip to content

Use a null pointer for empty data #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions tests/src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,19 +632,6 @@ void mbedtls_test_hexify(unsigned char *obuf,
}
}

unsigned char *mbedtls_test_zero_alloc(size_t len)
{
void *p;
size_t actual_len = (len != 0) ? len : 1;

p = mbedtls_calloc(1, actual_len);
TEST_HELPER_ASSERT(p != NULL);

memset(p, 0x00, actual_len);

return p;
}

unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
{
unsigned char *obuf;
Expand All @@ -653,7 +640,11 @@ unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
*olen = strlen(ibuf) / 2;

if (*olen == 0) {
return mbedtls_test_zero_alloc(*olen);
/* Any pointer should work for an empty buffer. Use NULL, to ensure
* that the library doesn't spuriously check for NULL. In Asan builds,
* we also validate that the library doesn't call functions such as
* memset() and memcpy() which reject NULL even for a size of 0. */
return NULL;
}

obuf = mbedtls_calloc(1, *olen);
Expand Down