Skip to content

Commit

Permalink
cheribsdtest: malloc_zero_size
Browse files Browse the repository at this point in the history
Check that the common malloc interfaces all return non-NULL for
requests of size 0.  This is part of the FreeBSD malloc ABI.
  • Loading branch information
brooksdavis committed Oct 10, 2023
1 parent 921e48a commit 266a094
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bin/cheribsdtest/cheribsdtest_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/

#include <stdlib.h>
#include <errno.h>
#include <malloc_np.h>

#include "cheribsdtest.h"

Expand Down Expand Up @@ -71,3 +73,47 @@ CHERIBSDTEST(malloc_revoke_twice, "revoke twice back to back",

cheribsdtest_success();
}

CHERIBSDTEST(malloc_zero_size,
"Check that allocators return non-NULL for size=0")
{
void *ptr, *ptr2;

CHERIBSDTEST_VERIFY((ptr = malloc(0)) != NULL);
free(ptr);

CHERIBSDTEST_VERIFY((ptr = calloc(0, 1)) != NULL);
free(ptr);
CHERIBSDTEST_VERIFY((ptr = calloc(1, 0)) != NULL);
free(ptr);
CHERIBSDTEST_VERIFY((ptr = calloc(0, 0)) != NULL);
free(ptr);

CHERIBSDTEST_VERIFY((ptr = realloc(NULL, 0)) != NULL);
CHERIBSDTEST_VERIFY((ptr2 = realloc(ptr, 0)) != NULL);
/*
* XXX: POSIX requires that: "A pointer to the allocated space
* shall be returned, and the memory object pointed to by ptr
* shall be freed." Unfortunately that's impractical to check as
* even with revocation the same storage could be allocated if
* revocation is triggered internally.
*/
free(ptr2);

/*
* C/POSIX require that aligned_alloc/posix_memalign take
* alignements that are a power-of-2 multiple of sizeof(void *).
*/
CHERIBSDTEST_VERIFY((ptr = aligned_alloc(sizeof(void *), 0)) != NULL);
free(ptr);

CHERIBSDTEST_VERIFY2(posix_memalign(&ptr, sizeof(void *), 0) == 0,
"posix_memalign failed, errno %d", errno);
CHERIBSDTEST_VERIFY2(ptr != NULL, "posix_memalign returned NULL");
free(ptr);

CHERIBSDTEST_VERIFY((ptr = memalign(sizeof(void *), 0)) != NULL);
free(ptr);

cheribsdtest_success();
}

0 comments on commit 266a094

Please sign in to comment.