]> Savannah Git Hosting - gnulib.git/commitdiff
xalloc: x2nrealloc check for ptrdiff_t overflow
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 29 Dec 2016 09:16:51 +0000 (01:16 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 29 Dec 2016 09:17:24 +0000 (01:17 -0800)
* lib/xalloc.h (x2nrealloc): Check for ptrdiff_t overflow, too.
* modules/xalloc, modules/xvasprintf (Depends-on): Add stdint.

ChangeLog
lib/xalloc.h
modules/xalloc
modules/xvasprintf

index b1f41bb0ffb342f623dbece0c3ddfdc53b1901f2..92dda3accea8db69a01840e19de0448f56709c40 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2016-12-29  Paul Eggert  <eggert@cs.ucla.edu>
+
+       xalloc: x2nrealloc check for ptrdiff_t overflow
+       * lib/xalloc.h (x2nrealloc): Check for ptrdiff_t overflow, too.
+       * modules/xalloc, modules/xvasprintf (Depends-on): Add stdint.
+
 2016-12-24  Bruno Haible  <bruno@clisp.org>
 
        lock test: Fix performance problem on multi-core machines.
index a4fddbfb5bb198f5c5ecaa4207904fd3a6cf8031..3aad4e3110ea042bab53cc9ee843424f355d0688 100644 (file)
@@ -19,6 +19,7 @@
 #define XALLOC_H_
 
 #include <stddef.h>
+#include <stdint.h>
 
 #include "xalloc-oversized.h"
 
@@ -197,10 +198,11 @@ x2nrealloc (void *p, size_t *pn, size_t s)
   else
     {
       /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0.
-         Check for overflow, so that N * S stays in size_t range.
-         The check may be slightly conservative, but an exact check isn't
-         worth the trouble.  */
-      if ((size_t) -1 / 3 * 2 / s <= n)
+         Check for overflow, so that N * S stays in both ptrdiff_t and
+         size_t range.  The check may be slightly conservative, but an
+         exact check isn't worth the trouble.  */
+      if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s
+          <= n)
         xalloc_die ();
       n += n / 2 + 1;
     }
index c6c20836b5b5019f7987b179738f73ca6c56643d..b107da93bbd05e7e07916a71bb62e72aedc7146e 100644 (file)
@@ -8,6 +8,7 @@ m4/xalloc.m4
 
 Depends-on:
 extern-inline
+stdint
 xalloc-die
 xalloc-oversized
 
index a931d7c9191ea31ebd8a4d5a373cec47abe874d4..410b45f0a799a33b6257e4f4d1031c985dfa6187 100644 (file)
@@ -17,6 +17,7 @@ vasprintf
 xalloc-die
 xsize
 stdarg
+stdint
 errno
 
 configure.ac: