]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
malloca: avoid ptrdiff_t overflow
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 21 Apr 2021 18:07:18 +0000 (11:07 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 21 Apr 2021 18:10:49 +0000 (11:10 -0700)
* lib/malloca.c: Include idx.h, intprops.h.
(mmalloca): Check for ptrdiff_t overflow.  Since this module uses
_GL_USE_STDLIB_ALLOC, it cannot assume GNU malloc semantics.
* modules/malloca (Depends-on): Add idx, intprops.

ChangeLog
lib/malloca.c
modules/malloca

index 1e6cbd07f27a37dbf5ef65253faad833e0a355e9..e72362077ebd60f2e14190c29309bc23b09834ef 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2021-04-21  Paul Eggert  <eggert@cs.ucla.edu>
 
+       malloca: avoid ptrdiff_t overflow
+       * lib/malloca.c: Include idx.h, intprops.h.
+       (mmalloca): Check for ptrdiff_t overflow.  Since this module uses
+       _GL_USE_STDLIB_ALLOC, it cannot assume GNU malloc semantics.
+       * modules/malloca (Depends-on): Add idx, intprops.
+
        careadlinkat: avoid ptrdiff_t overflow
        * lib/careadlinkat.c: Include idx.h, minmax.h.
        (readlink_stk): Avoid ptrdiff_t overflow in object allocation.
index f4ee1563b7a33e9da05df461fbaade9b21b4a4d1..4077bf708779ac07905735752b3f8904936036a9 100644 (file)
@@ -21,6 +21,8 @@
 /* Specification.  */
 #include "malloca.h"
 
+#include "idx.h"
+#include "intprops.h"
 #include "verify.h"
 
 /* The speed critical point in this file is freea() applied to an alloca()
@@ -45,9 +47,9 @@ mmalloca (size_t n)
 #if HAVE_ALLOCA
   /* Allocate one more word, used to determine the address to pass to freea(),
      and room for the alignment ≡ sa_alignment_max mod 2*sa_alignment_max.  */
-  size_t nplus = n + sizeof (small_t) + 2 * sa_alignment_max - 1;
-
-  if (nplus >= n)
+  int plus = sizeof (small_t) + 2 * sa_alignment_max - 1;
+  idx_t nplus;
+  if (!INT_ADD_WRAPV (n, plus, &nplus) && !xalloc_oversized (nplus, 1))
     {
       char *mem = (char *) malloc (nplus);
 
index 9b7a3dbd259976e5214dfd5cca788d3726b6988a..346d33251a74840c09590adaa4ea7395b245b07c 100644 (file)
@@ -9,6 +9,8 @@ m4/eealloc.m4
 
 Depends-on:
 alloca-opt
+idx
+intprops
 stdint
 verify
 xalloc-oversized