]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib:replace: Implement memset_explicit()
authorAndreas Schneider <asn@samba.org>
Thu, 16 Oct 2025 09:06:56 +0000 (11:06 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 11 Nov 2025 13:46:42 +0000 (13:46 +0000)
The memset_s() implementation is a bit obscure, as it requires a
constraint handler to be set up. You don't really find any
implmentations out there.

With C23 memset_explicit() was added and this has been implemented
for glibc 2.43 and also in FreeBSD.

See https://sourceware.org/bugzilla/show_bug.cgi?id=32378
See https://reviews.freebsd.org/D47286

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
lib/replace/README
lib/replace/replace.c
lib/replace/replace.h
lib/replace/wscript

index 13b7b13cb10f715a4e62fc972add76fde912697f..4c846e236681d21c6c7562bc48027c97d98f75ab 100644 (file)
@@ -72,6 +72,7 @@ symlink
 realpath
 poll
 setproctitle
+memset_explicit
 memset_s
 
 Types:
index e8ff990832263d43588c3cdc2a7e88380ae12c9c..232d1672e482b378ea8e6f0ae7a838bf3da3249b 100644 (file)
@@ -1131,6 +1131,19 @@ void rep_setproctitle_init(int argc, char *argv[], char *envp[])
 }
 #endif
 
+#ifndef HAVE_MEMSET_EXPLICIT
+void *rep_memset_explicit(void *block, int c, size_t size)
+{
+       void *ptr = memset(block, c, size);
+#ifdef HAVE_GCC_VOLATILE_MEMORY_PROTECTION
+       /* See http://llvm.org/bugs/show_bug.cgi?id=15495 */
+       __asm__ volatile("" : : "g"(block) : "memory");
+#endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
+
+       return ptr;
+}
+#endif
+
 #ifndef HAVE_MEMSET_S
 # ifndef RSIZE_MAX
 #  define RSIZE_MAX (SIZE_MAX >> 1)
index 4923e1f301d6db8cedf98187473ccf016e9e9506..21e6d2d960d4ca6fac732e50a368abf084acd459 100644 (file)
@@ -990,6 +990,11 @@ void rep_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
 void rep_setproctitle_init(int argc, char *argv[], char *envp[]);
 #endif
 
+#ifndef HAVE_MEMSET_EXPLICIT
+#define memset_explicit rep_memset_explicit
+void *rep_memset_explicit(void *block, int c, size_t size);
+#endif
+
 #ifndef HAVE_MEMSET_S
 #define memset_s rep_memset_s
 int rep_memset_s(void *dest, size_t destsz, int ch, size_t count);
index e351b5d19f2807bfce71e9f1a3e7f754fa37cc7d..1a78bf55f2a22bd40bb3300f1a08bfbfc8b17cb3 100644 (file)
@@ -890,7 +890,7 @@ REPLACEMENT_FUNCTIONS = {
                   'utime', 'utimes', 'dup2', 'chown', 'link', 'readlink',
                   'symlink', 'lchown', 'realpath', 'memmem', 'vdprintf',
                   'dprintf', 'get_current_dir_name', 'copy_file_range',
-                  'strerror_r', 'clock_gettime', 'memset_s'],
+                  'strerror_r', 'clock_gettime', 'memset_explicit', 'memset_s'],
     'timegm.c': ['timegm'],
     # Note: C99_VSNPRINTF is not a function, but a special condition
     # for replacement