From 4e2fa5b7cae13ca2d5f5d5d5c5fe653a43a6e252 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 4 Nov 2024 15:40:56 -0800 Subject: [PATCH] test: clang makes invalid assumptions about malloc and errno clang's internal malloc failure tests do not set errno, and so they fail to replicate the C library behavior. Skip errno tests when using clang. https://github.com/llvm/llvm-project/issues/114772 Signed-off-by: Keith Packard --- test/malloc.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/malloc.c b/test/malloc.c index cdc590f553..1a5089959f 100644 --- a/test/malloc.c +++ b/test/malloc.c @@ -53,6 +53,12 @@ #pragma GCC diagnostic ignored "-Warray-bounds" #endif +#ifdef __clang__ +#define CHECK_ERRNO() 0 +#else +#define CHECK_ERRNO() (errno != ENOMEM) +#endif + int main(void) { @@ -119,7 +125,7 @@ main(void) if (r) q = malloc(PTRDIFF_MAX); // printf("malloc(PTRDIFF_MAX: %p %p\n", r, q); - if ((r && q) || errno != ENOMEM) { + if ((r && q) || CHECK_ERRNO()) { printf("2*malloc(PTRDIFF_MAX) should have failed. got %p,%p error %s\n", r, q, strerror(errno)); result = 1; } @@ -129,7 +135,7 @@ main(void) errno = 0; r = malloc(SIZE_MAX); // printf("malloc(SIZE_MAX): %p\n", r); - if (r || errno != ENOMEM) { + if (r || CHECK_ERRNO()) { printf("malloc(SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno)); result = 1; } @@ -137,15 +143,16 @@ main(void) errno = 0; r = calloc(1, SIZE_MAX); // printf("calloc(1, SIZE_MAX): %p\n", r); - if (r || errno != ENOMEM) { + if (r || CHECK_ERRNO()) { printf("calloc(1, SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno)); result = 1; } + free(r); errno = 0; r = reallocarray(NULL, 1, SIZE_MAX); // printf("reallocarray(NULL, 1, SIZE_MAX): %p\n", r); - if (r || errno != ENOMEM) { + if (r || CHECK_ERRNO()) { printf("reallocarray(NULL, 1, SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno)); result = 1; } @@ -154,14 +161,14 @@ main(void) errno = 0; r = calloc(SIZE_MAX >> pow, SIZE_MAX >> pow); // printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r); - if (r || errno != ENOMEM) { + if (r || CHECK_ERRNO()) { printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n", pow, pow, r, strerror(errno)); result = 1; } free(r); r = reallocarray(NULL, SIZE_MAX >> pow, SIZE_MAX >> pow); // printf("reallocarray(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r); - if (r || errno != ENOMEM) { + if (r || CHECK_ERRNO()) { printf("reallocarray(NULL, SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n", pow, pow, r, strerror(errno)); result = 1; }