Hi!

Given the https://gcc.gnu.org/pipermail/libstdc++/2026-September/067867.html
changes, I had a brief look at gomp_aligned_alloc and omp_aligned_{,c}alloc.
At least when alignment is power of two, omp_aligned* seems to perform all the
overflow checking they should I think.  gomp_aligned_alloc is used internally,
if it uses posix_memalign or memalign, it relies on libc performing overflow
checking, for the aligned_alloc case it already performs overflow checking
itself.  Given the above libstdc++ change, seems at least Mingw _aligned_alloc
performs no overflow checking, so this patch adds overflow checking for it
(but relies on NULL/EINVAL if al is not a power of two from Mingw/Win).
And for the fallback also adds overflow checking.

Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk.

2026-09-23  Jakub Jelinek  <[email protected]>

        * alloc.c (gomp_aligned_alloc): Check for overflows in the
        _aligned_malloc case and in the last fallback.

--- a/libgomp/alloc.c   2026-03-27 10:17:22.294197583 +0100
+++ b/libgomp/alloc.c   2026-09-23 16:06:08.784540723 +0200
@@ -82,12 +82,22 @@ gomp_aligned_alloc (size_t al, size_t si
       ret = NULL;
   }
 #elif defined(HAVE__ALIGNED_MALLOC)
-  ret = _aligned_malloc (size, al);
+  /* Mingw64 doesn't perform overflow checking, so do it ourselves.
+     It does power of two checking though.  */
+  if (__builtin_add_overflow_p (size, al + 2 * sizeof (void *), (size_t) 0))
+    ret = NULL;
+  else
+    ret = _aligned_malloc (size, al);
 #else
   ret = NULL;
   if ((al & (al - 1)) == 0 && size)
     {
-      void *p = malloc (size + al);
+      void *p;
+      size_t sz;
+      if (__builtin_add_overflow (size, al, &sz))
+       p = NULL;
+      else
+       p = malloc (sz);
       if (p)
        {
          void *ap = (void *) (((uintptr_t) p + al) & -al);

        Jakub

Reply via email to