threads / patch / 64908

patchcocci: extend MEMZERO_ARRAY() rules

Subject: [PATCH] cocci: extend MEMZERO_ARRAY() rules

## tl;dr

2 messages between Feb 3, 2026 and Feb 3, 2026. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Toon Claes· Feb 3, 2026, 10:29 UTC · lore

In December we've added the MEMZERO_ARRAY() macro. I wrote the initial version, and Junio made improvements to it. But I realized not all cases where MEMZERO_ARRAY() was used were detected by coccinelle. So I've been improving the rules further. But due to the holidays I forgot had this patch laying around, and now during cleanup I ran into it again.

As far as I can tell, all cases that now use MEMZERO_ARRAY() would have been converted by coccinelle automatically.

As pointed out by René[1], "0x0" can be replaced by "0", and coccinelle can also be used to drop unneeded parentheses.

I'm not entirely sure it's worth applying this patch, as the original MEMZERO_ARRAY() date back a month or two. So I don't mind if this patch would be dismissed.

[1]: https://lore.kernel.org/git/f02b628f-b9d7-4436-88ee-3255e02cb0f3@web.de/

Kind regards, Toon

-- 8< --

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>
---
 contrib/coccinelle/array.cocci | 36 ++++++++++++++++++++++++++++------
 diffcore-delta.c               |  2 +-
 ewah/bitmap.c                  |  4 ++--
 3 files changed, 33 insertions(+), 9 deletions(-)
Show changes to 3 files +33 −10

contrib/coccinelle/array.cocci, diffcore-delta.c, ewah/bitmap.c

diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci
index d306f6a21e..e71baea00b 100644
--- a/contrib/coccinelle/array.cocci
+++ b/contrib/coccinelle/array.cocci
@@ -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)
diff --git a/diffcore-delta.c b/diffcore-delta.c
index 2de9e9ccff..2b7db39983 100644
--- a/diffcore-delta.c
+++ b/diffcore-delta.c
@@ -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;
diff --git a/ewah/bitmap.c b/ewah/bitmap.c
index bf878bf876..c378e0ab78 100644
--- a/ewah/bitmap.c
+++ b/ewah/bitmap.c
@@ -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);
--
2.53.0.rc1.267.g6e3a78c723
Junio C Hamano· Feb 3, 2026, 19:16 UTC · re: Toon Claes · lore

Re: [PATCH] cocci: extend MEMZERO_ARRAY() rules

Toon Claes <toon@iotcl.com> writes:
Show 37 quoted lines
> 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>
> ---
>  contrib/coccinelle/array.cocci | 36 ++++++++++++++++++++++++++++------
>  diffcore-delta.c               |  2 +-
>  ewah/bitmap.c                  |  4 ++--
>  3 files changed, 33 insertions(+), 9 deletions(-)
>
> diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci
> index d306f6a21e..e71baea00b 100644
> --- a/contrib/coccinelle/array.cocci
> +++ b/contrib/coccinelle/array.cocci
> @@ -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)
> +-                                               \) )

It is kind-a surprising that (n) with and without parentheses must be presented for the conversion to happen, while the tool clearly understands that 0 and 0x0 are equivalent ...

Show 11 quoted lines
> ++ MEMZERO_ARRAY(ptr, n)
> +
> +@@
> +type T;
> +T *ptr;
> +expression n;
> +@@
> +- memset(ptr, \( 0 \| '\0' \), \( sizeof(T)
> +-                              \| sizeof(ptr[...])
> +-                              \| sizeof(*ptr)
> +-                              \) * \( (n) \| n \) )

... and yet the tool does not understand the equivalence between '\0' and 0, nor (A * B) and (B * A). Puzzling, but OK.

And the pattern, with *ptr  substituted to ptr[], repeats.
Show 35 quoted lines
> diff --git a/diffcore-delta.c b/diffcore-delta.c
> index 2de9e9ccff..2b7db39983 100644
> --- a/diffcore-delta.c
> +++ b/diffcore-delta.c
> @@ -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;
> diff --git a/ewah/bitmap.c b/ewah/bitmap.c
> index bf878bf876..c378e0ab78 100644
> --- a/ewah/bitmap.c
> +++ b/ewah/bitmap.c
> @@ -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);
OK.  These do look easier to follow without extra parentheses.
Will queue.  Thanks.

← back to recent threads