Skip to content

Commit 08c95df

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7dbf3 commit 08c95df

File tree

3 files changed

+8
-19
lines changed

3 files changed

+8
-19
lines changed

ewah/bitmap.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program; if not, write to the Free Software
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20-
#include "git-compat-util.h"
20+
#include "cache.h"
2121
#include "ewok.h"
2222

2323
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
@@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)
3838
if (block >= self->word_alloc) {
3939
size_t old_size = self->word_alloc;
4040
self->word_alloc = block * 2;
41-
self->words = xrealloc(self->words,
42-
self->word_alloc * sizeof(eword_t));
43-
41+
REALLOC_ARRAY(self->words, self->word_alloc);
4442
memset(self->words + old_size, 0x0,
4543
(self->word_alloc - old_size) * sizeof(eword_t));
4644
}
@@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
10098
ewah_iterator_init(&it, ewah);
10199

102100
while (ewah_iterator_next(&blowup, &it)) {
103-
if (i >= bitmap->word_alloc) {
104-
bitmap->word_alloc *= 1.5;
105-
bitmap->words = xrealloc(
106-
bitmap->words, bitmap->word_alloc * sizeof(eword_t));
107-
}
108-
101+
ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
109102
bitmap->words[i++] = blowup;
110103
}
111104

@@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
134127

135128
if (self->word_alloc < other_final) {
136129
self->word_alloc = other_final;
137-
self->words = xrealloc(self->words,
138-
self->word_alloc * sizeof(eword_t));
130+
REALLOC_ARRAY(self->words, self->word_alloc);
139131
memset(self->words + original_size, 0x0,
140132
(self->word_alloc - original_size) * sizeof(eword_t));
141133
}

ewah/ewah_bitmap.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
3939
return;
4040

4141
self->alloc_size = new_size;
42-
self->buffer = xrealloc(self->buffer,
43-
self->alloc_size * sizeof(eword_t));
42+
REALLOC_ARRAY(self->buffer, self->alloc_size);
4443
self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
4544
}
4645

@@ -283,8 +282,8 @@ struct ewah_bitmap *ewah_new(void)
283282
struct ewah_bitmap *self;
284283

285284
self = xmalloc(sizeof(struct ewah_bitmap));
286-
self->buffer = xmalloc(32 * sizeof(eword_t));
287285
self->alloc_size = 32;
286+
ALLOC_ARRAY(self->buffer, self->alloc_size);
288287

289288
ewah_clear(self);
290289
return self;

ewah/ewah_io.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
134134
self->buffer_size = self->alloc_size = get_be32(ptr);
135135
ptr += sizeof(uint32_t);
136136

137-
self->buffer = xrealloc(self->buffer,
138-
self->alloc_size * sizeof(eword_t));
137+
REALLOC_ARRAY(self->buffer, self->alloc_size);
139138

140139
/*
141140
* Copy the raw data for the bitmap as a whole chunk;
@@ -177,8 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)
177176
return -1;
178177

179178
self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
180-
self->buffer = xrealloc(self->buffer,
181-
self->alloc_size * sizeof(eword_t));
179+
REALLOC_ARRAY(self->buffer, self->alloc_size);
182180

183181
/** 64 bit x N -- compressed words */
184182
buffer = self->buffer;

0 commit comments

Comments
 (0)