]> Kevux Git Server - rit/commitdiff
git-compat-util: introduce MEMZERO_ARRAY() macro
authorToon Claes <toon@iotcl.com>
Wed, 10 Dec 2025 13:13:01 +0000 (14:13 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Dec 2025 05:44:43 +0000 (14:44 +0900)
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>
builtin/last-modified.c
compat/simple-ipc/ipc-win32.c
contrib/coccinelle/array.cocci
diff-delta.c
ewah/bitmap.c
git-compat-util.h
hashmap.c
pack-revindex.c

index cc5fd2e..ac5387e 100644 (file)
@@ -327,7 +327,7 @@ static void process_parent(struct last_modified *lm,
        if (!(parent->object.flags & PARENT1))
                active_paths_free(lm, parent);
 
-       memset(lm->scratch->words, 0x0, lm->scratch->word_alloc * sizeof(eword_t));
+       MEMZERO_ARRAY(lm->scratch->words, lm->scratch->word_alloc);
        diff_queue_clear(&diff_queued_diff);
 }
 
index a8fc812..4a3e7df 100644 (file)
@@ -686,7 +686,7 @@ static LPSECURITY_ATTRIBUTES get_sa(struct my_sa_data *d)
                goto fail;
        }
 
-       memset(ea, 0, NR_EA * sizeof(EXPLICIT_ACCESS));
+       MEMZERO_ARRAY(ea, NR_EA);
 
        ea[0].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE;
        ea[0].grfAccessMode = SET_ACCESS;
index 27a3b47..d306f6a 100644 (file)
@@ -101,3 +101,23 @@ expression dst, src, n;
 -ALLOC_ARRAY(dst, n);
 -COPY_ARRAY(dst, src, n);
 +DUP_ARRAY(dst, src, n);
+
+@@
+type T;
+T *ptr;
+expression n;
+@@
+- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
+-                                 \| sizeof(*ptr)
+-                                 \) )
++ MEMZERO_ARRAY(ptr, n)
+
+@@
+type T;
+T[] ptr;
+expression n;
+@@
+- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
+-                                 \| sizeof(*ptr)
+-                                 \) )
++ MEMZERO_ARRAY(ptr, n)
index 71d3736..43c339f 100644 (file)
@@ -171,7 +171,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
        mem = hash + hsize;
        entry = mem;
 
-       memset(hash, 0, hsize * sizeof(*hash));
+       MEMZERO_ARRAY(hash, hsize);
 
        /* allocate an array to count hash entries */
        hash_count = calloc(hsize, sizeof(*hash_count));
index 55928da..bf878bf 100644 (file)
@@ -46,8 +46,7 @@ static void bitmap_grow(struct bitmap *self, size_t word_alloc)
 {
        size_t old_size = self->word_alloc;
        ALLOC_GROW(self->words, word_alloc, self->word_alloc);
-       memset(self->words + old_size, 0x0,
-              (self->word_alloc - old_size) * sizeof(eword_t));
+       MEMZERO_ARRAY(self->words + old_size, (self->word_alloc - old_size));
 }
 
 void bitmap_set(struct bitmap *self, size_t pos)
@@ -192,8 +191,8 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
        if (self->word_alloc < other_final) {
                self->word_alloc = other_final;
                REALLOC_ARRAY(self->words, self->word_alloc);
-               memset(self->words + original_size, 0x0,
-                       (self->word_alloc - original_size) * sizeof(eword_t));
+               MEMZERO_ARRAY(self->words + original_size,
+                             (self->word_alloc - original_size));
        }
 
        ewah_iterator_init(&it, other);
index 398e0fa..2b8192f 100644 (file)
@@ -726,6 +726,7 @@ static inline uint64_t u64_add(uint64_t a, uint64_t b)
 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
 #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
+#define MEMZERO_ARRAY(x, alloc) memset((x), 0x0, st_mult(sizeof(*(x)), (alloc)))
 
 #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
        BARF_UNLESS_COPYABLE((dst), (src)))
index a711377..3b5d6f1 100644 (file)
--- a/hashmap.c
+++ b/hashmap.c
@@ -194,7 +194,7 @@ void hashmap_partial_clear_(struct hashmap *map, ssize_t entry_offset)
                return;
        if (entry_offset >= 0)  /* called by hashmap_clear_entries */
                free_individual_entries(map, entry_offset);
-       memset(map->table, 0, map->tablesize * sizeof(struct hashmap_entry *));
+       MEMZERO_ARRAY(map->table, map->tablesize);
        map->shrink_at = 0;
        map->private_size = 0;
 }
index d0791cc..8598b94 100644 (file)
@@ -75,7 +75,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
        for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
                unsigned i;
 
-               memset(pos, 0, BUCKETS * sizeof(*pos));
+               MEMZERO_ARRAY(pos, BUCKETS);
 
                /*
                 * We want pos[i] to store the index of the last element that