1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-27 14:36:11 +02:00

ewah: convert to REALLOC_ARRAY, etc

Now that we're built around xmalloc and friends, we can use
helpers like REALLOC_ARRAY, ALLOC_GROW, and so on to make
the code shorter and protect against integer overflow.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2016-02-22 17:45:15 -05:00 committed by Junio C Hamano
parent fb7dbf3e7a
commit 08c95df8fa
3 changed files with 8 additions and 19 deletions

View File

@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "git-compat-util.h" #include "cache.h"
#include "ewok.h" #include "ewok.h"
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD)) #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)
if (block >= self->word_alloc) { if (block >= self->word_alloc) {
size_t old_size = self->word_alloc; size_t old_size = self->word_alloc;
self->word_alloc = block * 2; self->word_alloc = block * 2;
self->words = xrealloc(self->words, REALLOC_ARRAY(self->words, self->word_alloc);
self->word_alloc * sizeof(eword_t));
memset(self->words + old_size, 0x0, memset(self->words + old_size, 0x0,
(self->word_alloc - old_size) * sizeof(eword_t)); (self->word_alloc - old_size) * sizeof(eword_t));
} }
@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
ewah_iterator_init(&it, ewah); ewah_iterator_init(&it, ewah);
while (ewah_iterator_next(&blowup, &it)) { while (ewah_iterator_next(&blowup, &it)) {
if (i >= bitmap->word_alloc) { ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
bitmap->word_alloc *= 1.5;
bitmap->words = xrealloc(
bitmap->words, bitmap->word_alloc * sizeof(eword_t));
}
bitmap->words[i++] = blowup; bitmap->words[i++] = blowup;
} }
@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
if (self->word_alloc < other_final) { if (self->word_alloc < other_final) {
self->word_alloc = other_final; self->word_alloc = other_final;
self->words = xrealloc(self->words, REALLOC_ARRAY(self->words, self->word_alloc);
self->word_alloc * sizeof(eword_t));
memset(self->words + original_size, 0x0, memset(self->words + original_size, 0x0,
(self->word_alloc - original_size) * sizeof(eword_t)); (self->word_alloc - original_size) * sizeof(eword_t));
} }

View File

@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
return; return;
self->alloc_size = new_size; self->alloc_size = new_size;
self->buffer = xrealloc(self->buffer, REALLOC_ARRAY(self->buffer, self->alloc_size);
self->alloc_size * sizeof(eword_t));
self->rlw = self->buffer + (rlw_offset / sizeof(eword_t)); self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
} }
@ -283,8 +282,8 @@ struct ewah_bitmap *ewah_new(void)
struct ewah_bitmap *self; struct ewah_bitmap *self;
self = xmalloc(sizeof(struct ewah_bitmap)); self = xmalloc(sizeof(struct ewah_bitmap));
self->buffer = xmalloc(32 * sizeof(eword_t));
self->alloc_size = 32; self->alloc_size = 32;
ALLOC_ARRAY(self->buffer, self->alloc_size);
ewah_clear(self); ewah_clear(self);
return self; return self;

View File

@ -134,8 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
self->buffer_size = self->alloc_size = get_be32(ptr); self->buffer_size = self->alloc_size = get_be32(ptr);
ptr += sizeof(uint32_t); ptr += sizeof(uint32_t);
self->buffer = xrealloc(self->buffer, REALLOC_ARRAY(self->buffer, self->alloc_size);
self->alloc_size * sizeof(eword_t));
/* /*
* Copy the raw data for the bitmap as a whole chunk; * Copy the raw data for the bitmap as a whole chunk;
@ -177,8 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)
return -1; return -1;
self->buffer_size = self->alloc_size = (size_t)ntohl(word_count); self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
self->buffer = xrealloc(self->buffer, REALLOC_ARRAY(self->buffer, self->alloc_size);
self->alloc_size * sizeof(eword_t));
/** 64 bit x N -- compressed words */ /** 64 bit x N -- compressed words */
buffer = self->buffer; buffer = self->buffer;