]> Kevux Git Server - rit/commitdiff
cocci: extend MEMZERO_ARRAY() rules
authorToon Claes <toon@iotcl.com>
Tue, 3 Feb 2026 10:29:03 +0000 (11:29 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 3 Feb 2026 17:41:52 +0000 (09:41 -0800)
Recently the MEMZERO_ARRAY() macro was introduced. In that commit also
coccinelle rules were added to capture cases that can be converted to
use that macro.

Later a few more cases were manually converted to use the macro, but
coccinelle didn't capture those. Extend the rules to capture those as
well.

In various cases the code could be further beautified by removing
parentheses which are no longer needed. Modify the coccinelle rules to
optimize those as well and fix them.

During conversion indentation also used spaces where tabs should be
used, fix that in one go.

Signed-off-by: Toon Claes <toon@iotcl.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/coccinelle/array.cocci
diffcore-delta.c
ewah/bitmap.c

index d306f6a..e71baea 100644 (file)
@@ -107,9 +107,32 @@ type T;
 T *ptr;
 expression n;
 @@
-- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
--                                 \| sizeof(*ptr)
--                                 \) )
+- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T)
+-                                               \| sizeof(ptr[...])
+-                                               \| sizeof(*ptr)
+-                                               \) )
++ MEMZERO_ARRAY(ptr, n)
+
+@@
+type T;
+T *ptr;
+expression n;
+@@
+- memset(ptr, \( 0 \| '\0' \), \( sizeof(T)
+-                              \| sizeof(ptr[...])
+-                              \| sizeof(*ptr)
+-                              \) * \( (n) \| n \) )
++ MEMZERO_ARRAY(ptr, n)
+
+@@
+type T;
+T[] ptr;
+expression n;
+@@
+- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T)
+-                                               \| sizeof(ptr[...])
+-                                               \| sizeof(*ptr)
+-                                               \) )
 + MEMZERO_ARRAY(ptr, n)
 
 @@
@@ -117,7 +140,8 @@ type T;
 T[] ptr;
 expression n;
 @@
-- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
--                                 \| sizeof(*ptr)
--                                 \) )
+- memset(ptr, \( 0 \| '\0' \), \( sizeof(T)
+-                              \| sizeof(ptr[...])
+-                              \| sizeof(*ptr)
+-                              \) * \( (n) \| n \) )
 + MEMZERO_ARRAY(ptr, n)
index 2de9e9c..2b7db39 100644 (file)
@@ -135,7 +135,7 @@ static struct spanhash_top *hash_chars(struct repository *r,
                              st_mult(sizeof(struct spanhash), (size_t)1 << i)));
        hash->alloc_log2 = i;
        hash->free = INITIAL_FREE(i);
-       MEMZERO_ARRAY(hash->data, ((size_t)1 << i));
+       MEMZERO_ARRAY(hash->data, (size_t)1 << i);
 
        n = 0;
        accum1 = accum2 = 0;
index bf878bf..c378e0a 100644 (file)
@@ -46,7 +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);
-       MEMZERO_ARRAY(self->words + old_size, (self->word_alloc - old_size));
+       MEMZERO_ARRAY(self->words + old_size, self->word_alloc - old_size);
 }
 
 void bitmap_set(struct bitmap *self, size_t pos)
@@ -192,7 +192,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
                self->word_alloc = other_final;
                REALLOC_ARRAY(self->words, self->word_alloc);
                MEMZERO_ARRAY(self->words + original_size,
-                             (self->word_alloc - original_size));
+                             self->word_alloc - original_size);
        }
 
        ewah_iterator_init(&it, other);