git-compat-util: introduce MEMZERO_ARRAY() macro

Introduce a new macro MEMZERO_ARRAY() that zeroes the memory allocated by ALLOC_ARRAY() and friends. And add coccinelle rule to enforce the use of this macro. Signed-off-by: Toon Claes <toon@iotcl.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Toon Claes committed Dec 10, 2025 at 14:13 UTC a67b902c94a2f33275a3947a8bcdab03f64ae75e
8 files changed +29 -9
builtin/last-modified.c
+1 -1
@@ -327,7 +327,7 @@ static void process_parent(struct last_modified *lm,
327 if (!(parent->object.flags & PARENT1))
328 active_paths_free(lm, parent);
329
330 - memset(lm->scratch->words, 0x0, lm->scratch->word_alloc * sizeof(eword_t));
330 + MEMZERO_ARRAY(lm->scratch->words, lm->scratch->word_alloc);
331 diff_queue_clear(&diff_queued_diff);
332 }
333
compat/simple-ipc/ipc-win32.c
+1 -1
@@ -686,7 +686,7 @@ static LPSECURITY_ATTRIBUTES get_sa(struct my_sa_data *d)
686 goto fail;
687 }
688
689 - memset(ea, 0, NR_EA * sizeof(EXPLICIT_ACCESS));
689 + MEMZERO_ARRAY(ea, NR_EA);
690
691 ea[0].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE;
692 ea[0].grfAccessMode = SET_ACCESS;
contrib/coccinelle/array.cocci
+20
@@ -101,3 +101,23 @@ expression dst, src, n;
101 -ALLOC_ARRAY(dst, n);
102 -COPY_ARRAY(dst, src, n);
103 +DUP_ARRAY(dst, src, n);
104 +
105 +@@
106 +type T;
107 +T *ptr;
108 +expression n;
109 +@@
110 +- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
111 +- \| sizeof(*ptr)
112 +- \) )
113 ++ MEMZERO_ARRAY(ptr, n)
114 +
115 +@@
116 +type T;
117 +T[] ptr;
118 +expression n;
119 +@@
120 +- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
121 +- \| sizeof(*ptr)
122 +- \) )
123 ++ MEMZERO_ARRAY(ptr, n)
diff-delta.c
+1 -1
@@ -171,7 +171,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
171 mem = hash + hsize;
172 entry = mem;
173
174 - memset(hash, 0, hsize * sizeof(*hash));
174 + MEMZERO_ARRAY(hash, hsize);
175
176 /* allocate an array to count hash entries */
177 hash_count = calloc(hsize, sizeof(*hash_count));
ewah/bitmap.c
+3 -4
@@ -46,8 +46,7 @@ static void bitmap_grow(struct bitmap *self, size_t word_alloc)
46 {
47 size_t old_size = self->word_alloc;
48 ALLOC_GROW(self->words, word_alloc, self->word_alloc);
49 - memset(self->words + old_size, 0x0,
50 - (self->word_alloc - old_size) * sizeof(eword_t));
49 + MEMZERO_ARRAY(self->words + old_size, (self->word_alloc - old_size));
50 }
51
52 void bitmap_set(struct bitmap *self, size_t pos)
@@ -192,8 +191,8 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
191 if (self->word_alloc < other_final) {
192 self->word_alloc = other_final;
193 REALLOC_ARRAY(self->words, self->word_alloc);
195 - memset(self->words + original_size, 0x0,
196 - (self->word_alloc - original_size) * sizeof(eword_t));
194 + MEMZERO_ARRAY(self->words + original_size,
195 + (self->word_alloc - original_size));
196 }
197
198 ewah_iterator_init(&it, other);
git-compat-util.h
+1
@@ -726,6 +726,7 @@ static inline uint64_t u64_add(uint64_t a, uint64_t b)
726 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
727 #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
728 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
729 +#define MEMZERO_ARRAY(x, alloc) memset((x), 0x0, st_mult(sizeof(*(x)), (alloc)))
730
731 #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
732 BARF_UNLESS_COPYABLE((dst), (src)))
hashmap.c
+1 -1
@@ -194,7 +194,7 @@ void hashmap_partial_clear_(struct hashmap *map, ssize_t entry_offset)
194 return;
195 if (entry_offset >= 0) /* called by hashmap_clear_entries */
196 free_individual_entries(map, entry_offset);
197 - memset(map->table, 0, map->tablesize * sizeof(struct hashmap_entry *));
197 + MEMZERO_ARRAY(map->table, map->tablesize);
198 map->shrink_at = 0;
199 map->private_size = 0;
200 }
pack-revindex.c
+1 -1
@@ -75,7 +75,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
75 for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
76 unsigned i;
77
78 - memset(pos, 0, BUCKETS * sizeof(*pos));
78 + MEMZERO_ARRAY(pos, BUCKETS);
79
80 /*
81 * We want pos[i] to store the index of the last element that