From 05e4db63d044ee235d2fbfab8b0bb9fbdfb18315 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Sat, 30 May 2020 14:02:56 +0200 Subject: [PATCH] openmp: omp_alloc(0, ...) should return NULL. 2020-05-30 Jakub Jelinek * allocator.c (omp_alloc): For size == 0, return NULL early. * testsuite/libgomp.c-c++-common/alloc-4.c: New test. --- libgomp/allocator.c | 3 +++ .../testsuite/libgomp.c-c++-common/alloc-4.c | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 libgomp/testsuite/libgomp.c-c++-common/alloc-4.c diff --git a/libgomp/allocator.c b/libgomp/allocator.c index 8592de6e2b5..66308ab6669 100644 --- a/libgomp/allocator.c +++ b/libgomp/allocator.c @@ -201,6 +201,9 @@ omp_alloc (size_t size, omp_allocator_handle_t allocator) size_t alignment, new_size; void *ptr, *ret; + if (__builtin_expect (size == 0, 0)) + return NULL; + retry: if (allocator == omp_null_allocator) { diff --git a/libgomp/testsuite/libgomp.c-c++-common/alloc-4.c b/libgomp/testsuite/libgomp.c-c++-common/alloc-4.c new file mode 100644 index 00000000000..841e1bcc201 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/alloc-4.c @@ -0,0 +1,25 @@ +#include +#include + +const omp_alloctrait_t traits[] += { { omp_atk_pool_size, 1 }, + { omp_atk_fallback, omp_atv_abort_fb } }; + +int +main () +{ + omp_allocator_handle_t a; + + if (omp_alloc (0, omp_null_allocator) != NULL) + abort (); + a = omp_init_allocator (omp_default_mem_space, 2, traits); + if (a != omp_null_allocator) + { + if (omp_alloc (0, a) != NULL + || omp_alloc (0, a) != NULL + || omp_alloc (0, a) != NULL) + abort (); + omp_destroy_allocator (a); + } + return 0; +} -- 2.30.2