From 06b9291b4a351c5d3a40f6c80ee11713840b1039 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@samba.org>
Date: Thu, 16 Oct 2025 11:22:46 +0200
Subject: [PATCH 4/4] lib:replace: Remove memset_s()

Upstream-Status: Backport [https://gitlab.com/samba-team/samba/-/commit/f3b380b8a3866286244725903287211cf54a4e74]
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>

Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org>
Autobuild-Date(master): Tue Nov 11 14:51:45 UTC 2025 on atb-devel-224

Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 lib/replace/README                      |  1 -
 lib/replace/replace.c                   | 87 ++++++++-----------------
 lib/replace/replace.h                   |  5 --
 lib/replace/wscript                     |  2 +-
 third_party/heimdal_build/roken.h       |  4 --
 third_party/heimdal_build/wscript_build |  5 ++
 6 files changed, 34 insertions(+), 70 deletions(-)

diff --git a/lib/replace/README b/lib/replace/README
index bb9d008..d8431e7 100644
--- a/lib/replace/README
+++ b/lib/replace/README
@@ -73,7 +73,6 @@ realpath
 poll
 setproctitle
 memset_explicit
-memset_s
 
 Types:
 bool
diff --git a/lib/replace/replace.c b/lib/replace/replace.c
index 8615899..a419837 100644
--- a/lib/replace/replace.c
+++ b/lib/replace/replace.c
@@ -965,37 +965,6 @@
 }
 #endif

-#ifndef HAVE_MEMSET_S
-# ifndef RSIZE_MAX
-#  define RSIZE_MAX (SIZE_MAX >> 1)
-# endif
-
-int rep_memset_s(void *dest, size_t destsz, int ch, size_t count)
-{
-	if (dest == NULL) {
-		return EINVAL;
-	}
-
-	if (destsz > RSIZE_MAX ||
-	    count > RSIZE_MAX ||
-	    count > destsz) {
-		return ERANGE;
-	}
-
-#if defined(HAVE_MEMSET_EXPLICIT)
-	memset_explicit(dest, ch, count);
-#else /* HAVE_MEMSET_EXPLICIT */
-	memset(dest, ch, count);
-# if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION)
-	/* See http://llvm.org/bugs/show_bug.cgi?id=15495 */
-	__asm__ volatile("" : : "g"(dest) : "memory");
-# endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */
-#endif /* HAVE_MEMSET_EXPLICIT */
-
-	return 0;
-}
-#endif /* HAVE_MEMSET_S */
-
 #ifndef HAVE_GETPROGNAME
 # ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
 # define PROGNAME_SIZE 32
diff --git a/lib/replace/replace.h b/lib/replace/replace.h
index 6b72c1f..24b2d8c 100644
--- a/lib/replace/replace.h
+++ b/lib/replace/replace.h
@@ -999,11 +999,6 @@ void rep_setproctitle_init(int argc, char *argv[], char *envp[]);
 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);
-#endif
-
 #ifndef HAVE_GETPROGNAME
 #define getprogname rep_getprogname
 const char *rep_getprogname(void);
diff --git a/lib/replace/wscript b/lib/replace/wscript
index 9acebd3..faef185 100644
--- a/lib/replace/wscript
+++ b/lib/replace/wscript
@@ -881,7 +881,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_explicit', 'memset_s'],
+                  'strerror_r', 'clock_gettime', 'memset_explicit'],
     'timegm.c': ['timegm'],
     # Note: C99_VSNPRINTF is not a function, but a special condition
     # for replacement
diff --git a/third_party/heimdal_build/roken.h b/third_party/heimdal_build/roken.h
index 3870609..4740a3d 100644
--- a/third_party/heimdal_build/roken.h
+++ b/third_party/heimdal_build/roken.h
@@ -123,10 +123,6 @@
 #define HAVE_SETEUID
 #endif
 
-#ifndef HAVE_MEMSET_S
-#define HAVE_MEMSET_S
-#endif
-
 #ifndef HAVE_DIRFD
 #ifdef HAVE_DIR_DD_FD
 #define dirfd(x) ((x)->dd_fd)
diff --git a/third_party/heimdal_build/wscript_build b/third_party/heimdal_build/wscript_build
index 8aea52b..1988535 100644
--- a/third_party/heimdal_build/wscript_build
+++ b/third_party/heimdal_build/wscript_build
@@ -366,6 +366,11 @@ if not bld.CONFIG_SET('USING_SYSTEM_ROKEN'):
         lib/roken/getuserinfo.c
     '''
 
+    if not bld.CONFIG_SET('HAVE_MEMSET_S'):
+        ROKEN_SOURCE += '''
+            lib/roken/memset_s.c
+        '''
+
     HEIMDAL_LIBRARY('roken',
         ROKEN_SOURCE,
         includes='../heimdal/lib/roken ../heimdal/include ../heimdal_build/include',
