bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: [PROPOSED 1/4] memset_explicit: new module
Date: Mon, 28 Nov 2022 17:17:10 +0100	[thread overview]
Message-ID: <3038533.U3zVgo479M@nimes> (raw)
In-Reply-To: <20221128045543.1355731-2-eggert@cs.ucla.edu>

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	other threads:[~2022-11-28 16:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-28  4:55 [PROPOSED 0/4] memset_explicit patches Paul Eggert
2022-11-28  4:55 ` [PROPOSED 1/4] memset_explicit: new module Paul Eggert
2022-11-28 16:17   ` Bruno Haible [this message]
2022-11-28  4:55 ` [PROPOSED 2/4] read-file: use memset_explicit Paul Eggert
2022-11-28  4:55 ` [PROPOSED 3/4] explicit_bzero: memset_explicit is standard Paul Eggert
2022-11-28  4:55 ` [PROPOSED 4/4] explicit_bzero: implement via memset_explicit Paul Eggert
2022-11-28 16:17   ` Bruno Haible
2022-11-29  6:06     ` Paul Eggert
2022-11-29  8:09       ` Bruno Haible
2022-11-28 10:15 ` [PROPOSED 0/4] memset_explicit patches Simon Josefsson via Gnulib discussion list
2022-11-28 16:04   ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3038533.U3zVgo479M@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).