]> Savannah Git Hosting - gnulib.git/commitdiff
memset_explicit: Guarantee N3322 functionality.
authorBruno Haible <bruno@clisp.org>
Wed, 6 Nov 2024 12:02:09 +0000 (13:02 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 6 Nov 2024 12:02:09 +0000 (13:02 +0100)
* m4/memset_explicit.m4 (gl_PREREQ_MEMSET_EXPLICIT): Require
AC_CANONICAL_HOST. Test whether memset_s supports zero-length
operations.
* lib/memset_explicit.c (memset_explicit): Don't invoke memset_s with
argument 0 if memset_s does not support this.

ChangeLog
lib/memset_explicit.c
m4/memset_explicit.m4

index 1ed9fa07631bff42998b593da82e3804bfd4aaa3..b28c659afeaed1fceee898d5db6fd1f45d9f7571 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-11-06  Bruno Haible  <bruno@clisp.org>
+
+       memset_explicit: Guarantee N3322 functionality.
+       * m4/memset_explicit.m4 (gl_PREREQ_MEMSET_EXPLICIT): Require
+       AC_CANONICAL_HOST. Test whether memset_s supports zero-length
+       operations.
+       * lib/memset_explicit.c (memset_explicit): Don't invoke memset_s with
+       argument 0 if memset_s does not support this.
+
 2024-11-06  Bruno Haible  <bruno@clisp.org>
 
        wcsncat: Fix configure test.
index 33c098734820e2d04a852768a12ae5edf839fabc..a5d2b00b7ea24db1d9033e816fc88f9336bfe4fe 100644 (file)
@@ -27,7 +27,10 @@ memset_explicit (void *s, int c, size_t len)
 #if HAVE_EXPLICIT_MEMSET
   return explicit_memset (s, c, len);
 #elif HAVE_MEMSET_S
-  (void) memset_s (s, len, c, len);
+# 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);
index 85b0c915d25f3030340ece4134713e8e8d6c4a6c..abdabffcfa9276de09342148545346dcc4fdb1f2 100644 (file)
@@ -1,5 +1,5 @@
 # memset_explicit.m4
-# serial 3
+# serial 4
 dnl Copyright 2022-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -23,6 +23,36 @@ AC_DEFUN([gl_FUNC_MEMSET_EXPLICIT],
 
 AC_DEFUN([gl_PREREQ_MEMSET_EXPLICIT],
 [
+  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
   AC_CHECK_FUNCS([explicit_memset])
   AC_CHECK_FUNCS_ONCE([memset_s])
+  if test $ac_cv_func_memset_s = yes; then
+    AC_CACHE_CHECK([for working memset_s],
+      [gl_cv_func_memset_s_works],
+      [AC_RUN_IFELSE(
+         [AC_LANG_PROGRAM(
+            [[#include <string.h>
+              #include <stddef.h>
+            ]], [[
+              (void) memset_s (NULL, 0, '?', 0);
+            ]])
+         ],
+         [gl_cv_func_memset_s_works=yes],
+         [gl_cv_func_memset_s_works=no],
+         [case "$host_os" in
+            # Guess no on Solaris.
+            solaris*)
+              gl_cv_func_memset_s_works="guessing no" ;;
+            *)
+              gl_cv_func_memset_s_works="guessing yes" ;;
+          esac
+         ])
+      ])
+    case "$gl_cv_func_memset_s_works" in
+      *yes)
+        AC_DEFINE([HAVE_MEMSET_S_SUPPORTS_ZERO], [1],
+          [Define to 1 if memset_s support zero-length operations.])
+        ;;
+    esac
+  fi
 ])