[PATCH] Hide 0 size passed to memset/bzero from compiler

Sam James sam@gentoo.org
Tue Dec 17 15:01:57 GMT 2024


"H.J. Lu" <hjl.tools@gmail.com> writes:

> Hide 0 size passed to memset/bzero from compiler since compiler may reject
> 0 size.  Clang 19 issues an error in such case:
>
> ./tester.c:1345:29: error: 'size' argument to memset is '0'; did you mean to transpose the last two arguments? [-Werror,-Wmemset-transposed-args]
>  1345 |   (void) memset(one+2, 'y', 0);
>       |                             ^
> ./tester.c:1345:29: note: parenthesize the third argument to silence
> ./tester.c:1432:16: error: 'size' argument to bzero is '0' [-Werror,-Wsuspicious-bzero]
>  1432 |   bzero(one+2, 0);
>       |                ^
> ./tester.c:1432:16: note: parenthesize the second argument to silence
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>

OK, although I don't know if we want to do the similar trick as for
*alloc. But not required.

> ---
>  string/tester.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/string/tester.c b/string/tester.c
> index ee96747280..ae0c6a5541 100644
> --- a/string/tester.c
> +++ b/string/tester.c
> @@ -69,6 +69,10 @@ DIAG_IGNORE_NEEDS_COMMENT (7, "-Wstringop-overflow=");
>  const char *it = "<UNSET>";	/* Routine name for message routines. */
>  size_t errors = 0;
>  
> +/* NB: Hide 0 size from compiler since compiler may reject 0 size passed
> +   to memset and bzero.  */
> +size_t size_zero = 0;
> +
>  /* Complain if condition is not true.  */
>  static void
>  check (int thing, int number)
> @@ -1342,7 +1346,7 @@ test_memset (void)
>    check(memset(one+1, 'x', 3) == one+1, 1);	/* Return value. */
>    equal(one, "axxxefgh", 2);		/* Basic test. */
>  
> -  (void) memset(one+2, 'y', 0);
> +  (void) memset(one+2, 'y', size_zero);
>    equal(one, "axxxefgh", 3);		/* Zero-length set. */
>  
>    (void) memset(one+5, 0, 1);
> @@ -1429,7 +1433,7 @@ test_bzero (void)
>    equal(one+4, "ef", 3);
>  
>    (void) strcpy(one, "abcdef");
> -  bzero(one+2, 0);
> +  bzero(one+2, size_zero);
>    equal(one, "abcdef", 4);		/* Zero-length copy. */
>  }


More information about the Libc-alpha mailing list