summaryrefslogtreecommitdiff
path: root/string
diff options
context:
space:
mode:
Diffstat (limited to 'string')
-rw-r--r--string/Makefile1
-rw-r--r--string/string.h21
-rw-r--r--string/test-strnlen.c1
-rw-r--r--string/tst-const.c107
4 files changed, 130 insertions, 0 deletions
diff --git a/string/Makefile b/string/Makefile
index d842ae0457..ffa6590b3e 100644
--- a/string/Makefile
+++ b/string/Makefile
@@ -196,6 +196,7 @@ tests := \
tester \
tst-bswap \
tst-cmp \
+ tst-const \
tst-endian \
tst-inlcall \
tst-memmove-overflow \
diff --git a/string/string.h b/string/string.h
index 81f0b7fd21..1dc5995c17 100644
--- a/string/string.h
+++ b/string/string.h
@@ -113,6 +113,10 @@ memchr (const void *__s, int __c, size_t __n) __THROW
#else
extern void *memchr (const void *__s, int __c, size_t __n)
__THROW __attribute_pure__ __nonnull ((1));
+# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
+# define memchr(S, C, N) \
+ __glibc_const_generic (S, const void *, memchr (S, C, N))
+# endif
#endif
#ifdef __USE_GNU
@@ -252,6 +256,10 @@ strchr (const char *__s, int __c) __THROW
#else
extern char *strchr (const char *__s, int __c)
__THROW __attribute_pure__ __nonnull ((1));
+# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
+# define strchr(S, C) \
+ __glibc_const_generic (S, const char *, strchr (S, C))
+# endif
#endif
/* Find the last occurrence of C in S. */
#ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
@@ -279,6 +287,10 @@ strrchr (const char *__s, int __c) __THROW
#else
extern char *strrchr (const char *__s, int __c)
__THROW __attribute_pure__ __nonnull ((1));
+# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
+# define strrchr(S, C) \
+ __glibc_const_generic (S, const char *, strrchr (S, C))
+# endif
#endif
#ifdef __USE_MISC
@@ -329,6 +341,10 @@ strpbrk (const char *__s, const char *__accept) __THROW
#else
extern char *strpbrk (const char *__s, const char *__accept)
__THROW __attribute_pure__ __nonnull ((1, 2));
+# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
+# define strpbrk(S, ACCEPT) \
+ __glibc_const_generic (S, const char *, strpbrk (S, ACCEPT))
+# endif
#endif
/* Find the first occurrence of NEEDLE in HAYSTACK. */
#ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
@@ -356,6 +372,11 @@ strstr (const char *__haystack, const char *__needle) __THROW
#else
extern char *strstr (const char *__haystack, const char *__needle)
__THROW __attribute_pure__ __nonnull ((1, 2));
+# if __GLIBC_USE (ISOC23) && defined __glibc_const_generic && !defined _LIBC
+# define strstr(HAYSTACK, NEEDLE) \
+ __glibc_const_generic (HAYSTACK, const char *, \
+ strstr (HAYSTACK, NEEDLE))
+# endif
#endif
diff --git a/string/test-strnlen.c b/string/test-strnlen.c
index acb1c055fc..7b5ceaf941 100644
--- a/string/test-strnlen.c
+++ b/string/test-strnlen.c
@@ -63,6 +63,7 @@ IMPL (__strnlen_default, 1)
# define libc_hidden_weak(a)
# include "wcsmbs/wmemchr.c"
# define WCSNLEN __wcsnlen_default
+# undef wmemchr
# define wmemchr __wmemchr_default
# include "wcsmbs/wcsnlen.c"
IMPL (__wcsnlen_default, 1)
diff --git a/string/tst-const.c b/string/tst-const.c
new file mode 100644
index 0000000000..a7c8ad5aad
--- /dev/null
+++ b/string/tst-const.c
@@ -0,0 +1,107 @@
+/* Test <string.h> const-generic macros.
+ Copyright (C) 2025 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/>. */
+
+#include <string.h>
+#include <libc-diag.h>
+
+void *vp;
+const void *cvp;
+int *ip;
+const int *cip;
+char *cp;
+const char *ccp;
+int c;
+size_t sz;
+
+#define CHECK_TYPE(EXPR, TYPE) \
+ _Static_assert (_Generic (EXPR, TYPE: 1), "type check")
+
+static int
+do_test (void)
+{
+ /* This is a compilation test. */
+ CHECK_TYPE (memchr (vp, c, sz), void *);
+ CHECK_TYPE (memchr (cvp, c, sz), const void *);
+ CHECK_TYPE (memchr (ip, c, sz), void *);
+ CHECK_TYPE (memchr (cip, c, sz), const void *);
+ CHECK_TYPE (memchr (cp, c, sz), void *);
+ CHECK_TYPE (memchr (ccp, c, sz), const void *);
+ DIAG_PUSH_NEEDS_COMMENT;
+ /* This deliberately tests the type of the result with a null
+ pointer constant argument. */
+ DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
+ CHECK_TYPE (memchr (0, c, sz), void *);
+ CHECK_TYPE (memchr ((void *) 0, c, sz), void *);
+ DIAG_POP_NEEDS_COMMENT;
+ CHECK_TYPE ((memchr) (cvp, c, sz), void *);
+ CHECK_TYPE (strchr (vp, c), char *);
+ CHECK_TYPE (strchr (cvp, c), const char *);
+ CHECK_TYPE (strchr (cp, c), char *);
+ CHECK_TYPE (strchr (ccp, c), const char *);
+ DIAG_PUSH_NEEDS_COMMENT;
+ DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
+ CHECK_TYPE (strchr (0, c), char *);
+ CHECK_TYPE (strchr ((void *) 0, c), char *);
+ DIAG_POP_NEEDS_COMMENT;
+ CHECK_TYPE ((strchr) (ccp, c), char *);
+ CHECK_TYPE (strpbrk (vp, vp), char *);
+ CHECK_TYPE (strpbrk (vp, cvp), char *);
+ CHECK_TYPE (strpbrk (cvp, vp), const char *);
+ CHECK_TYPE (strpbrk (cvp, cvp), const char *);
+ CHECK_TYPE (strpbrk (cp, cp), char *);
+ CHECK_TYPE (strpbrk (cp, ccp), char *);
+ CHECK_TYPE (strpbrk (ccp, cp), const char *);
+ CHECK_TYPE (strpbrk (ccp, ccp), const char *);
+ DIAG_PUSH_NEEDS_COMMENT;
+ DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
+ CHECK_TYPE (strpbrk (0, cp), char *);
+ CHECK_TYPE (strpbrk (0, ccp), char *);
+ CHECK_TYPE (strpbrk ((void *) 0, cp), char *);
+ CHECK_TYPE (strpbrk ((void *) 0, ccp), char *);
+ DIAG_POP_NEEDS_COMMENT;
+ CHECK_TYPE ((strpbrk) (ccp, ccp), char *);
+ CHECK_TYPE (strrchr (vp, c), char *);
+ CHECK_TYPE (strrchr (cvp, c), const char *);
+ CHECK_TYPE (strrchr (cp, c), char *);
+ CHECK_TYPE (strrchr (ccp, c), const char *);
+ DIAG_PUSH_NEEDS_COMMENT;
+ DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
+ CHECK_TYPE (strrchr (0, c), char *);
+ CHECK_TYPE (strrchr ((void *) 0, c), char *);
+ DIAG_POP_NEEDS_COMMENT;
+ CHECK_TYPE ((strrchr) (ccp, c), char *);
+ CHECK_TYPE (strstr (vp, vp), char *);
+ CHECK_TYPE (strstr (vp, cvp), char *);
+ CHECK_TYPE (strstr (cvp, vp), const char *);
+ CHECK_TYPE (strstr (cvp, cvp), const char *);
+ CHECK_TYPE (strstr (cp, cp), char *);
+ CHECK_TYPE (strstr (cp, ccp), char *);
+ CHECK_TYPE (strstr (ccp, cp), const char *);
+ CHECK_TYPE (strstr (ccp, ccp), const char *);
+ DIAG_PUSH_NEEDS_COMMENT;
+ DIAG_IGNORE_NEEDS_COMMENT (14, "-Wnonnull");
+ CHECK_TYPE (strstr (0, cp), char *);
+ CHECK_TYPE (strstr (0, ccp), char *);
+ CHECK_TYPE (strstr ((void *) 0, cp), char *);
+ CHECK_TYPE (strstr ((void *) 0, ccp), char *);
+ DIAG_POP_NEEDS_COMMENT;
+ CHECK_TYPE ((strstr) (ccp, ccp), char *);
+ return 0;
+}
+
+#include <support/test-driver.c>