cocci: extend MEMZERO_ARRAY() rules
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>
Toon Claes committed
Feb 3, 2026 at 11:29 UTC
60614838a44591c1449f939236f396bb7164b5ef
3 files changed
+33
-9
contrib/coccinelle/array.cocci
+30
-6
@@ -107,9 +107,32 @@ type T;
107
T *ptr;
108
expression n;
109
@@
110
-- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
111
-- \| sizeof(*ptr)
112
-- \) )
110
+- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T)
111
+- \| sizeof(ptr[...])
112
+- \| sizeof(*ptr)
113
+- \) )
114
++ MEMZERO_ARRAY(ptr, n)
115
+
116
+@@
117
+type T;
118
+T *ptr;
119
+expression n;
120
+@@
121
+- memset(ptr, \( 0 \| '\0' \), \( sizeof(T)
122
+- \| sizeof(ptr[...])
123
+- \| sizeof(*ptr)
124
+- \) * \( (n) \| n \) )
125
++ MEMZERO_ARRAY(ptr, n)
126
+
127
+@@
128
+type T;
129
+T[] ptr;
130
+expression n;
131
+@@
132
+- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T)
133
+- \| sizeof(ptr[...])
134
+- \| sizeof(*ptr)
135
+- \) )
136
+ MEMZERO_ARRAY(ptr, n)
137
138
@@
@@ -117,7 +140,8 @@ type T;
140
T[] ptr;
141
expression n;
142
@@
120
-- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
121
-- \| sizeof(*ptr)
122
-- \) )
143
+- memset(ptr, \( 0 \| '\0' \), \( sizeof(T)
144
+- \| sizeof(ptr[...])
145
+- \| sizeof(*ptr)
146
+- \) * \( (n) \| n \) )
147
+ MEMZERO_ARRAY(ptr, n)
diffcore-delta.c
+1
-1
@@ -135,7 +135,7 @@ static struct spanhash_top *hash_chars(struct repository *r,
135
st_mult(sizeof(struct spanhash), (size_t)1 << i)));
136
hash->alloc_log2 = i;
137
hash->free = INITIAL_FREE(i);
138
- MEMZERO_ARRAY(hash->data, ((size_t)1 << i));
138
+ MEMZERO_ARRAY(hash->data, (size_t)1 << i);
139
140
n = 0;
141
accum1 = accum2 = 0;
ewah/bitmap.c
+2
-2
@@ -46,7 +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
- MEMZERO_ARRAY(self->words + old_size, (self->word_alloc - old_size));
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,7 +192,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
192
self->word_alloc = other_final;
193
REALLOC_ARRAY(self->words, self->word_alloc);
194
MEMZERO_ARRAY(self->words + original_size,
195
- (self->word_alloc - original_size));
195
+ self->word_alloc - original_size);
196
}
197
198
ewah_iterator_init(&it, other);