[PATCH] string: Declare POSIX.1-2024 string functions [BZ #34466]

Gao Xiang <[email protected]>
Newsgroups gmane.comp.lib.glibc.alpha
Message-ID <[email protected]>
From: Xiang Gao <[email protected]>

POSIX.1-2024 adds memmem, strlcpy and strlcat to string.h.
glibc already implements these functions. but their public
declarations are currently only exports by __USE_MISC.
Applications built in POSIX.1-2024 mode with _POSIX_C_SOURCE=
202405L will fail with implicit function declaration errors.

Using existed feature macro to expose the declarations required
by POSIX.1-2024.

Add string/tst-posix2024 to verify strict POSIX.1-2024 header
visibility.

Tested on x86_64 Fedora 42.

The following tests passed without regressions:
  make test t=string/tst-posix2024
  make check subdirs=string
  make check subdirs=conform

Signed-off-by: Xiang Gao <[email protected]>
---
 conform/data/string.h-data |  7 ++++++
 string/Makefile            |  2 ++
 string/string.h            |  6 ++++--
 string/tst-posix2024.c     | 44 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 string/tst-posix2024.c

diff --git a/conform/data/string.h-data b/conform/data/string.h-data
index abc6b12b5d..c9526e06d0 100644
--- a/conform/data/string.h-data
+++ b/conform/data/string.h-data
@@ -13,6 +13,9 @@ function int memcmp (const void*, const void*, size_t)
 function {void*} memcpy (void*, const void*, size_t)
 function {void*} memmove (void*, const void*, size_t)
 function {void*} memset (void*, int, size_t)
+#if defined XOPEN2K24 || defined POSIX2024
+function {void*} memmem (const void*, size_t, const void*, size_t)
+#endif
 #if defined XOPEN2K8 || defined XOPEN2K24 || defined POSIX2008 || defined POSIX2024
 function {char*} stpcpy (char*, const char*)
 function {char*} stpncpy (char*, const char*, size_t)
@@ -38,6 +41,10 @@ function int strerror_r (int, char*, size_t)
 function {char*} strerror_l (int, locale_t)
 #endif
 function size_t strlen (const char*)
+#if defined XOPEN2K24 || defined POSIX2024
+function size_t strlcat (char*, const char*, size_t)
+function size_t strlcpy (char*, const char*, size_t)
+#endif
 function {char*} strncat (char*, const char*, size_t)
 function int strncmp (const char*, const char*, size_t)
 function {char*} strncpy (char*, const char*, size_t)
diff --git a/string/Makefile b/string/Makefile
index aa0b0c2f57..a968994fcb 100644
--- a/string/Makefile
+++ b/string/Makefile
@@ -201,6 +201,7 @@ tests := \
   tst-endian \
   tst-inlcall \
   tst-memmove-overflow \
+  tst-posix2024 \
   tst-strerror-fail \
   tst-strfry \
   tst-strlcat \
@@ -270,6 +271,7 @@ CFLAGS-tst-inlcall.c += -fno-builtin
 CFLAGS-tst-xbzero-opt.c += -O3
 CFLAGS-tst-xmemset-opt.c += -O3
 CFLAGS-test-endian-sign-conversion.c += -Werror -Wsign-conversion
+CFLAGS-tst-posix2024.c += -std=c17 -Werror=implicit-function-declaration
 # BZ 21006: Resolve all functions but at least explicit_bzero at startup.
 # Otherwise the test fails on s390x as the memcpy in prepare_test_buffer is
 # done by loading r4 / r5 with the test_pattern and using store multiple
diff --git a/string/string.h b/string/string.h
index 743395b3e3..15959356a2 100644
--- a/string/string.h
+++ b/string/string.h
@@ -414,7 +414,7 @@ extern char *strcasestr (const char *__haystack, const char *__needle)
 # endif
 #endif
 
-#ifdef __USE_MISC
+#if defined __USE_MISC || defined __USE_XOPEN2K24
 /* Find the first occurrence of NEEDLE in HAYSTACK.
    NEEDLE is NEEDLELEN bytes long;
    HAYSTACK is HAYSTACKLEN bytes long.  */
@@ -423,7 +423,9 @@ extern void *memmem (const void *__haystack, size_t __haystacklen,
      __THROW __attribute_pure__ __nonnull ((1, 3))
     __attr_access ((__read_only__, 1, 2))
     __attr_access ((__read_only__, 3, 4));
+#endif
 
+#ifdef __USE_MISC
 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
    last written byte.  */
 extern void *__mempcpy (void *__restrict __dest,
@@ -533,7 +535,7 @@ extern char *stpncpy (char *__restrict __dest,
      __THROW __nonnull ((1, 2));
 #endif
 
-#ifdef __USE_MISC
+#if defined __USE_MISC || defined __USE_XOPEN2K24
 /* Copy at most N - 1 characters from SRC to DEST.  */
 extern size_t strlcpy (char *__restrict __dest,
 		       const char *__restrict __src, size_t __n)
diff --git a/string/tst-posix2024.c b/string/tst-posix2024.c
new file mode 100644
index 0000000000..0e25d0770f
--- /dev/null
+++ b/string/tst-posix2024.c
@@ -0,0 +1,44 @@
+/* Test POSIX.1-2024 <string.h> declarations.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library 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.
+
+   The GNU C Library 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 the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Undo _GNU_SOURCE define before the first public header is included so that
+   this test checks strict POSIX.1-2024 visibility.  */
+
+#undef _GNU_SOURCE
+#define _POSIX_C_SOURCE 202405L
+
+#include <string.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  char buffer[8];
+
+  TEST_COMPARE (strlcpy (buffer, "abc", sizeof buffer), 3);
+  TEST_COMPARE_STRING (buffer, "abc");
+
+  TEST_COMPARE (strlcat (buffer, "defgh", sizeof buffer), 8);
+  TEST_COMPARE_STRING (buffer, "abcdefg");
+
+  TEST_VERIFY (memmem (buffer, strlen (buffer), "cde", 3) == buffer + 2);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.