]> Savannah Git Hosting - gnulib.git/commitdiff
bitset: check memory allocation
authorAkim Demaille <akim.demaille@gmail.com>
Thu, 5 Sep 2019 09:36:39 +0000 (11:36 +0200)
committerAkim Demaille <akim.demaille@gmail.com>
Sun, 8 Sep 2019 06:38:49 +0000 (08:38 +0200)
Reported by 江 祖铭 (Zu-Ming Jiang).
With help from Paul Eggert.
https://lists.gnu.org/archive/html/bug-bison/2019-08/msg00016.html

* lib/bitset/table.c (tbitset_resize): When growing, use xrealloc
instead of realloc.
When shrinking, accept failures.
* lib/bitset/vector.c (vbitset_resize): Likewise.

ChangeLog
lib/bitset/table.c
lib/bitset/vector.c

index 28a6b264b3baf26dda20f14b2a94d340e4766235..077471de997061033f1888207268e0c2c57b1328 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2019-09-06  Akim Demaille  <akim@lrde.epita.fr>
+
+       bitset: check memory allocation
+       Reported by 江 祖铭 (Zu-Ming Jiang).
+       With help from Paul Eggert.
+       https://lists.gnu.org/archive/html/bug-bison/2019-08/msg00016.html
+       * lib/bitset/table.c (tbitset_resize): When growing, use xrealloc
+       instead of realloc.
+       When shrinking, accept failures.
+       * lib/bitset/vector.c (vbitset_resize): Likewise.
+
 2019-09-07  Paul Eggert  <eggert@cs.ucla.edu>
 
        scratch_buffer: sync from glibc
index 07184d657480e040e6ac1248c69ee1a27c755746..7691d9805d22e8c552fa3b337e587f323d43eba3 100644 (file)
@@ -25,6 +25,7 @@
 #include <string.h>
 
 #include "obstack.h"
+#include "xalloc.h"
 
 /* This file implements expandable bitsets.  These bitsets can be of
    arbitrary length and are more efficient than arrays of bits for
@@ -142,7 +143,7 @@ tbitset_resize (bitset src, bitset_bindex n_bits)
 
           bitset_windex size = oldsize == 0 ? newsize : newsize + newsize / 4;
           EBITSET_ELTS (src)
-            = realloc (EBITSET_ELTS (src), size * sizeof (tbitset_elt *));
+            = xrealloc (EBITSET_ELTS (src), size * sizeof (tbitset_elt *));
           EBITSET_ASIZE (src) = size;
         }
 
@@ -155,9 +156,13 @@ tbitset_resize (bitset src, bitset_bindex n_bits)
          the memory unless it is shrinking by a reasonable amount.  */
       if ((oldsize - newsize) >= oldsize / 2)
         {
-          EBITSET_ELTS (src)
+          void *p
             = realloc (EBITSET_ELTS (src), newsize * sizeof (tbitset_elt *));
-          EBITSET_ASIZE (src) = newsize;
+          if (p)
+            {
+              EBITSET_ELTS (src) = p;
+              EBITSET_ASIZE (src) = newsize;
+            }
         }
 
       /* Need to prune any excess bits.  FIXME.  */
index 54f148d568a84f3e66b2ca3e09db4ae136f2e987..5e543283a271cf8d4f872f1ab1b31070639ad149 100644 (file)
@@ -24,6 +24,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "xalloc.h"
+
 /* This file implements variable size bitsets stored as a variable
    length array of words.  Any unused bits in the last word must be
    zero.
@@ -74,7 +76,7 @@ vbitset_resize (bitset src, bitset_bindex n_bits)
 
           bitset_windex size = oldsize == 0 ? newsize : newsize + newsize / 4;
           VBITSET_WORDS (src)
-            = realloc (VBITSET_WORDS (src), size * sizeof (bitset_word));
+            = xrealloc (VBITSET_WORDS (src), size * sizeof (bitset_word));
           VBITSET_ASIZE (src) = size;
         }
 
@@ -88,9 +90,13 @@ vbitset_resize (bitset src, bitset_bindex n_bits)
          the memory unless it is shrinking by a reasonable amount.  */
       if ((oldsize - newsize) >= oldsize / 2)
         {
-          VBITSET_WORDS (src)
+          void *p
             = realloc (VBITSET_WORDS (src), newsize * sizeof (bitset_word));
-          VBITSET_ASIZE (src) = newsize;
+          if (p)
+            {
+              VBITSET_WORDS (src) = p;
+              VBITSET_ASIZE (src) = newsize;
+            }
         }
 
       /* Need to prune any excess bits.  FIXME.  */