]> Savannah Git Hosting - gnulib.git/commitdiff
xalloc: do not exceed PTRDIFF_MAX
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Dec 2016 17:59:21 +0000 (09:59 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 15 Dec 2016 18:19:48 +0000 (10:19 -0800)
* lib/xmalloc.c (xcalloc) [HAVE_GNU_CALLOC]: Do not omit
xalloc_oversized check, since objects larger than PTRDIFF_MAX
bytes have pointer-subtraction problems.

ChangeLog
lib/xmalloc.c

index 07c4d6539662440898013cc80216bc0c8daa3589..bee9a1728e381eb7b076baa035ab8146c343ebd0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2016-12-15  Paul Eggert  <eggert@cs.ucla.edu>
 
+       xalloc: do not exceed PTRDIFF_MAX
+       * lib/xmalloc.c (xcalloc) [HAVE_GNU_CALLOC]: Do not omit
+       xalloc_oversized check, since objects larger than PTRDIFF_MAX
+       bytes have pointer-subtraction problems.
+
        malloca: do not exceed PTRDIFF_MAX
        * lib/malloca.h: Include xalloc-oversized.
        (nmalloca): Use xalloc_oversized instead of rolling our own.
index 429b50d9946c7fc8d3919845066d1aa629624912..7d9c0777297d71d69386301077b22f165742f4f8 100644 (file)
@@ -93,11 +93,11 @@ void *
 xcalloc (size_t n, size_t s)
 {
   void *p;
-  /* Test for overflow, since some calloc implementations don't have
-     proper overflow checks.  But omit overflow and size-zero tests if
-     HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
-     returns NULL if successful.  */
-  if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
+  /* Test for overflow, since objects with size greater than
+     PTRDIFF_MAX cause pointer subtraction to go awry.  Omit size-zero
+     tests if HAVE_GNU_CALLOC, since GNU calloc never returns NULL if
+     successful.  */
+  if (xalloc_oversized (n, s)
       || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
     xalloc_die ();
   return p;