File: memset_explicit.c

package info (click to toggle)
gettext 0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 167,652 kB
  • sloc: ansic: 532,868; sh: 68,216; perl: 28,011; makefile: 9,046; lisp: 3,184; yacc: 1,055; java: 615; cs: 589; cpp: 397; objc: 343; sed: 79; tcl: 63; xml: 40; pascal: 11; awk: 7; php: 7
file content (54 lines) | stat: -rw-r--r-- 1,930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Erase sensitive data from memory.
   Copyright 2022-2024 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include <string.h>

/* Set S's bytes to C, where S has LEN bytes.  The compiler will not
   optimize effects away, even if S is dead after the call.  */
void *
memset_explicit (void *s, int c, size_t len)
{
#if HAVE_EXPLICIT_MEMSET
  return explicit_memset (s, c, len);
#elif HAVE_MEMSET_S
# if !HAVE_MEMSET_S_SUPPORTS_ZERO
  if (len > 0)
# endif
    (void) memset_s (s, len, c, len);
  return s;
#elif defined __GNUC__ && !defined __clang__
  memset (s, c, len);
  /* Compiler barrier.  */
  __asm__ volatile ("" ::: "memory");
  return s;
#elif defined __clang__
  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");
  return s;
#else
  /* Invoke memset through a volatile function pointer.  This defeats compiler
     optimizations.  */
  void * (* const volatile volatile_memset) (void *, int, size_t) = memset;
  return volatile_memset (s, c, len);
#endif
}