bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PROPOSED 1/4] memset_explicit: new module


From: Bruno Haible
Subject: Re: [PROPOSED 1/4] memset_explicit: new module
Date: Mon, 28 Nov 2022 17:17:10 +0100

Paul Eggert wrote:
In lib/memset_explicit.c:
> +#if HAVE_EXPLICIT_MEMSET
> +  return explicit_memset (s, '\0', len);

'\0' should be c here.

> +#elif HAVE_MEMSET_S
> +  (void) memset_s (s, len, '\0', len);

Likewise.

> +#elif defined __GNUC__ && !defined __clang__
> +  return memset (s, c, len);
> +  /* Compiler barrier. */
> +  __asm__ volatile ("" ::: "memory");

I don't think a compiler barrier in a dead-code position has any effect.
I would therefore write this as

  memset (s, c, len);
  /* Compiler barrier. */
  __asm__ volatile ("" ::: "memory");
  return s;

> +#elif defined __clang__
> +  return memset (s, c, len);
> +  /* Compiler barrier. */
> +  /* With asm ("" ::: "memory") LLVM analyzes uses of 's' and finds that the
> +     whole thing is dead and eliminates it.  Use 'g' to work around this
> +     problem.  See <https://bugs.llvm.org/show_bug.cgi?id=15495#c11>.  */
> +  __asm__ volatile ("" : : "g"(s) : "memory");

Likewise.

In tests/test-memset_explicit.c:
> +static void
> +test_static (void)
> +{
> +  memcpy (stbuf, SECRET, SECRET_SIZE);
> +  memset_explicit (stbuf, 0, SECRET_SIZE);
> +  ASSERT (memcmp (zero, stbuf, SECRET_SIZE) == 0);
> +  for (int i = 1; i <= UCHAR_MAX; i++)
> +    {
> +      char checkbuf[SECRET_SIZE];
> +      memset (checkbuf, i, SECRET_SIZE);
> +      memset_explicit (stbuf, i, SECRET_SIZE);
> +      ASSERT (memcmp (checkbuf, stbuf, SECRET_SIZE) == 0);
> +    }
> +}

I don't understand the purpose of this line:
         memset (checkbuf, i, SECRET_SIZE);
Wouldn't it be better to have
         memcpy (stbuf, SECRET, SECRET_SIZE);
instead?

Bruno






reply via email to

[Prev in Thread] Current Thread [Next in Thread]