]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
openmp: omp_alloc(0, ...) should return NULL.
authorTobias Burnus <tobias@codesourcery.com>
Wed, 15 Jul 2020 07:55:58 +0000 (09:55 +0200)
committerTobias Burnus <tobias@codesourcery.com>
Wed, 15 Jul 2020 07:55:58 +0000 (09:55 +0200)
2020-05-30  Jakub Jelinek  <jakub@redhat.com>

* allocator.c (omp_alloc): For size == 0, return NULL early.

* testsuite/libgomp.c-c++-common/alloc-4.c: New test.

(cherry picked from commit 05e4db63d044ee235d2fbfab8b0bb9fbdfb18315)

libgomp/ChangeLog.omp
libgomp/allocator.c
libgomp/testsuite/libgomp.c-c++-common/alloc-4.c [new file with mode: 0644]

index 79b404cf2091a1d03c3896dade87ef5e4b1676a1..13c3f22ef5008571db53266a3bd8f4d1200060be 100644 (file)
@@ -1,3 +1,10 @@
+2020-07-15  Tobias Burnus  <tobias@codesourcery.com>
+
+       Backport from mainline
+       2020-05-30  Jakub Jelinek  <jakub@redhat.com>
+
+       * allocator.c (omp_alloc): For size == 0, return NULL early.
+
 2020-07-15  Tobias Burnus  <tobias@codesourcery.com>
 
        Backport from mainline
index 8592de6e2b5b3a763a8c1280c2c310d140779499..66308ab666968b906427b9242fb44a6fec55e390 100644 (file)
@@ -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 (file)
index 0000000..841e1bc
--- /dev/null
@@ -0,0 +1,25 @@
+#include <omp.h>
+#include <stdlib.h>
+
+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;
+}