From 8174382fe7a278309fc98b6e11ff99b6f41a8719 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@samba.org>
Date: Thu, 16 Oct 2025 11:19:51 +0200
Subject: [PATCH 3/4] Replace memset_s() with memset_explicit()

Upstream-Status: Backport [https://gitlab.com/samba-team/samba/-/commit/3e81b73a050e511c658afc786478431ceef175ee]
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 lib/replace/replace.h                    | 18 +++++++++---------
 lib/util/memory.h                        | 16 +++++++++-------
 lib/util/tests/test_talloc_keep_secret.c | 19 ++++++++-----------
 3 files changed, 26 insertions(+), 27 deletions(-)

--- a/lib/replace/replace.h
+++ b/lib/replace/replace.h
@@ -811,50 +811,50 @@ typedef unsigned long long ptrdiff_t ;
 /**
  * Zero a structure.
  */
-#define ZERO_STRUCT(x) memset_s((char *)&(x), sizeof(x), 0, sizeof(x))
+#define ZERO_STRUCT(x) memset_explicit((char *)&(x), 0, sizeof(x))
 
 /**
  * Zero a structure given a pointer to the structure.
  */
 #define ZERO_STRUCTP(x) do { \
 	if ((x) != NULL) { \
-		memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x))); \
+		memset_explicit((char *)(x), 0, sizeof(*(x))); \
 	} \
 } while(0)
 
 /**
  * Zero a structure given a pointer to the structure - no zero check
  */
-#define ZERO_STRUCTPN(x) memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x)))
+#define ZERO_STRUCTPN(x) memset_explicit((char *)(x), 0, sizeof(*(x)))
 
 /**
  * Zero an array - note that sizeof(array) must work - ie. it must not be a
  * pointer
  */
-#define ZERO_ARRAY(x) memset_s((char *)(x), sizeof(x), 0, sizeof(x))
+#define ZERO_ARRAY(x) memset_explicit((char *)(x), 0, sizeof(x))
 
 /**
  * Zero a given len of an array
  */
-#define ZERO_ARRAY_LEN(x, l) memset_s((char *)(x), (l), 0, (l))
+#define ZERO_ARRAY_LEN(x, l) memset_explicit((char *)(x), 0, (l))
 
 /**
  * Explicitly zero data from memory. This is guaranteed to be not optimized
  * away.
  */
-#define BURN_DATA(x) memset_s((char *)&(x), sizeof(x), 0, sizeof(x))
+#define BURN_DATA(x) memset_explicit((char *)&(x), 0, sizeof(x))
 
 /**
  * Explicitly zero data from memory. This is guaranteed to be not optimized
  * away.
  */
-#define BURN_DATA_SIZE(x, s) memset_s((char *)&(x), (s), 0, (s))
+#define BURN_DATA_SIZE(x, s) memset_explicit((char *)&(x), 0, (s))
 
 /**
  * Explicitly zero data from memory. This is guaranteed to be not optimized
  * away.
  */
-#define BURN_PTR_SIZE(x, s) memset_s((x), (s), 0, (s))
+#define BURN_PTR_SIZE(x, s) memset_explicit((x), 0, (s))
 
 /**
  * Explicitly zero data in string. This is guaranteed to be not optimized
@@ -863,7 +863,7 @@ typedef unsigned long long ptrdiff_t ;
 #define BURN_STR(x)	do { \
 				if ((x) != NULL) { \
 					size_t s = strlen(x); \
-					memset_s((x), s, 0, s); \
+					memset_explicit((x), 0, s); \
 				} \
 			} while(0)
 
--- a/lib/util/memory.h
+++ b/lib/util/memory.h
@@ -40,7 +40,7 @@
 #define BURN_FREE_STR(x) do { \
 				if ((x) != NULL) { \
 					size_t s = strlen(x); \
-					memset_s((x), s, 0, s); \
+					memset_explicit((x), 0, s); \
 					free(x); (x) = NULL; \
 				} \
 			} while(0)
@@ -53,7 +53,7 @@
  **/
 #define BURN_FREE(x, s) do { \
 				if ((x) != NULL) { \
-					memset_s((x), (s), 0, (s)); \
+					memset_explicit((x), 0, (s)); \
 					free(x); (x) = NULL; \
 				} \
 			} while(0)
@@ -78,7 +78,7 @@
  * Zero a structure.
  */
 #ifndef ZERO_STRUCT
-#define ZERO_STRUCT(x) memset_s((char *)&(x), sizeof(x), 0, sizeof(x))
+#define ZERO_STRUCT(x) memset_explicit((char *)&(x), 0, sizeof(x))
 #endif
 
 /**
@@ -87,7 +87,7 @@
 #ifndef ZERO_STRUCTP
 #define ZERO_STRUCTP(x) do { \
 	if ((x) != NULL) { \
-		memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x))); \
+		memset_explicit((char *)(x), 0, sizeof(*(x))); \
 	} \
 } while(0)
 #endif
@@ -96,7 +96,7 @@
  * Zero a structure given a pointer to the structure - no zero check.
  */
 #ifndef ZERO_STRUCTPN
-#define ZERO_STRUCTPN(x) memset_s((char *)(x), sizeof(*(x)), 0, sizeof(*(x)))
+#define ZERO_STRUCTPN(x) memset_explicit((char *)(x), 0, sizeof(*(x)))
 #endif
 
 /**
@@ -104,13 +104,15 @@
  * pointer.
  */
 #ifndef ZERO_ARRAY
-#define ZERO_ARRAY(x) memset_s((char *)(x), sizeof(x), 0, sizeof(x))
+#define ZERO_ARRAY(x) memset_explicit((char *)(x), 0, sizeof(x))
 #endif
 
 /**
  * Zero a given len of an array
  */
-#define ZERO_ARRAY_LEN(x, l) memset_s((char *)(x), (l), 0, (l))
+#ifndef ZERO_ARRAY_LEN
+#define ZERO_ARRAY_LEN(x, l) memset_explicit((char *)(x), 0, (l))
+#endif
 
 /**
  * Work out how many elements there are in a static array
--- a/lib/util/tests/test_talloc_keep_secret.c
+++ b/lib/util/tests/test_talloc_keep_secret.c
@@ -8,12 +8,11 @@
 #include <talloc.h>
 #include "lib/util/talloc_keep_secret.h"
 
-int rep_memset_s(void *dest, size_t destsz, int ch, size_t count);
+int rep_memset_explicit(void *dest, int ch, size_t count);
 
-int rep_memset_s(void *dest, size_t destsz, int ch, size_t count)
+int rep_memset_explicit(void *dest, int ch, size_t count)
 {
 	check_expected_ptr(dest);
-	check_expected(destsz);
 	check_expected(ch);
 	check_expected(count);
 
@@ -44,10 +43,9 @@ static void test_talloc_keep_secret(void
 	ptr1_size = talloc_get_size(ptr1);
 	assert_int_equal(ptr1_size, strlen(ptr1) + 1);
 
-	expect_string(rep_memset_s, dest, "secret");
-	expect_value(rep_memset_s, destsz, strlen(ptr1) + 1);
-	expect_value(rep_memset_s, ch, (int)'\0');
-	expect_value(rep_memset_s, count, strlen(ptr1) + 1);
+	expect_string(rep_memset_explicit, dest, "secret");
+	expect_value(rep_memset_explicit, ch, (int)'\0');
+	expect_value(rep_memset_explicit, count, strlen(ptr1) + 1);
 
 	talloc_free(ptr1);
 
@@ -73,10 +71,9 @@ static void test_talloc_keep_secret_vali
 	assert_non_null(password);
 	talloc_keep_secret(password);
 
-	expect_string(rep_memset_s, dest, "secret");
-	expect_value(rep_memset_s, destsz, strlen(password) + 1);
-	expect_value(rep_memset_s, ch, (int)'\0');
-	expect_value(rep_memset_s, count, strlen(password) + 1);
+	expect_string(rep_memset_explicit, dest, "secret");
+	expect_value(rep_memset_explicit, ch, (int)'\0');
+	expect_value(rep_memset_explicit, count, strlen(password) + 1);
 
 	talloc_free(mem_ctx);
 }
