From e8ae66371aa41fea74efaaa37687b50440875866 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 11 Jul 2017 22:39:46 +0200 Subject: [PATCH] Fix issue detected by new test. --- m-bitset.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/m-bitset.h b/m-bitset.h index 081d8191..9de230c8 100644 --- a/m-bitset.h +++ b/m-bitset.h @@ -199,13 +199,19 @@ bitset_reserve (bitset_t v, size_t size) if (oldAlloc > newAlloc) { newAlloc = oldAlloc; } - bitset_limb *ptr = M_MEMORY_REALLOC (bitset_limb, v->ptr, newAlloc); - if (M_UNLIKELY (ptr == NULL) ) { - M_MEMORY_FULL(newAlloc * sizeof(bitset_limb)); - return; + if (M_UNLIKELY (newAlloc == 0)) { + M_MEMORY_FREE (v->ptr); + v->size = v->alloc = 0; + v->ptr = NULL; + } else { + bitset_limb *ptr = M_MEMORY_REALLOC (bitset_limb, v->ptr, newAlloc); + if (M_UNLIKELY (ptr == NULL) ) { + M_MEMORY_FULL(newAlloc * sizeof(bitset_limb)); + return; + } + v->ptr = ptr; + v->alloc = newAlloc; } - v->ptr = ptr; - v->alloc = newAlloc; BITSETI_CONTRACT (v); }