[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
- [PROPOSED 0/4] memset_explicit patches, Paul Eggert, 2022/11/27
- [PROPOSED 2/4] read-file: use memset_explicit, Paul Eggert, 2022/11/27
- [PROPOSED 3/4] explicit_bzero: memset_explicit is standard, Paul Eggert, 2022/11/27
- [PROPOSED 4/4] explicit_bzero: implement via memset_explicit, Paul Eggert, 2022/11/27
- [PROPOSED 1/4] memset_explicit: new module, Paul Eggert, 2022/11/27
- Re: [PROPOSED 1/4] memset_explicit: new module,
Bruno Haible <=
- Re: [PROPOSED 0/4] memset_explicit patches, Simon Josefsson, 2022/11/28