From d3bb911fd0f53b2c69c96da43cc0f6557f3d2dd8 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 5 Sep 2019 11:36:39 +0200 Subject: [PATCH] bitset: check memory allocation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 11 +++++++++++ lib/bitset/table.c | 11 ++++++++--- lib/bitset/vector.c | 12 +++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 28a6b264b3..077471de99 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2019-09-06 Akim Demaille + + 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 scratch_buffer: sync from glibc diff --git a/lib/bitset/table.c b/lib/bitset/table.c index 07184d6574..7691d9805d 100644 --- a/lib/bitset/table.c +++ b/lib/bitset/table.c @@ -25,6 +25,7 @@ #include #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. */ diff --git a/lib/bitset/vector.c b/lib/bitset/vector.c index 54f148d568..5e543283a2 100644 --- a/lib/bitset/vector.c +++ b/lib/bitset/vector.c @@ -24,6 +24,8 @@ #include #include +#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. */ -- 2.39.5