Module Name:    src
Committed By:   riastradh
Date:           Fri Nov  1 21:11:37 UTC 2024

Modified Files:
        src/common/lib/libc/string: strlcat.c strlcpy.c
        src/include: string.h
        src/sys/lib/libkern: libkern.h

Log Message:
string.h: Fix various symbol visibility issues.

1. Order declarations according to POSIX 2024 to make this easier to
   review side-by-side with the spec.
2. Fix visibility of memccpy: XSI-only, not POSIX in general; require
   _XOPEN_SOURCE, not just _POSIX_C_SOURCE.
3. Omit redundant _XOPEN_SOURCE test around stpcpy/stpncpy.
4. Hide strdup in POSIX 2001.  Not POSIX (without XSI) until 2008.
5. Hide strerror_r until POSIX 2001.  Can't find evidence of it in
   any earlier POSIX or X/Open.  (Not 100% sure on this one, maybe
   someone can double-check my research.)
6. Add restrict to strlcat/strlcpy.
7. Omit redundant _XOPEN_SOURCE test around strndup and strnlen.
8. Hide strtok_r until POSIX 2001.  Can't find evidence of it in
   any earlier POSIX or X/Open.  (Not 100% sure on this one, maybe
   someone can double-check my research.)

Carry the restrict qualifiers on strlcat/strlcpy to libkern too.

Main reference:
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/string.h.html

PR standards/58804: string.h: wrong visibility for memccpy


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/common/lib/libc/string/strlcat.c
cvs rdiff -u -r1.3 -r1.4 src/common/lib/libc/string/strlcpy.c
cvs rdiff -u -r1.55 -r1.56 src/include/string.h
cvs rdiff -u -r1.146 -r1.147 src/sys/lib/libkern/libkern.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libc/string/strlcat.c
diff -u src/common/lib/libc/string/strlcat.c:1.4 src/common/lib/libc/string/strlcat.c:1.5
--- src/common/lib/libc/string/strlcat.c:1.4	Wed Jan 23 07:57:27 2013
+++ src/common/lib/libc/string/strlcat.c	Fri Nov  1 21:11:37 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: strlcat.c,v 1.4 2013/01/23 07:57:27 matt Exp $	*/
+/*	$NetBSD: strlcat.c,v 1.5 2024/11/01 21:11:37 riastradh Exp $	*/
 /*	$OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $	*/
 
 /*
@@ -24,7 +24,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: strlcat.c,v 1.4 2013/01/23 07:57:27 matt Exp $");
+__RCSID("$NetBSD: strlcat.c,v 1.5 2024/11/01 21:11:37 riastradh Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #ifdef _LIBC
@@ -53,7 +53,7 @@ __weak_alias(strlcat, _strlcat)
  * If retval >= siz, truncation occurred.
  */
 size_t
-strlcat(char *dst, const char *src, size_t siz)
+strlcat(char *__restrict dst, const char *__restrict src, size_t siz)
 {
 #if 1
 	char *d = dst;

Index: src/common/lib/libc/string/strlcpy.c
diff -u src/common/lib/libc/string/strlcpy.c:1.3 src/common/lib/libc/string/strlcpy.c:1.4
--- src/common/lib/libc/string/strlcpy.c:1.3	Mon Jun  4 18:19:27 2007
+++ src/common/lib/libc/string/strlcpy.c	Fri Nov  1 21:11:37 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: strlcpy.c,v 1.3 2007/06/04 18:19:27 christos Exp $	*/
+/*	$NetBSD: strlcpy.c,v 1.4 2024/11/01 21:11:37 riastradh Exp $	*/
 /*	$OpenBSD: strlcpy.c,v 1.7 2003/04/12 21:56:39 millert Exp $	*/
 
 /*
@@ -24,7 +24,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: strlcpy.c,v 1.3 2007/06/04 18:19:27 christos Exp $");
+__RCSID("$NetBSD: strlcpy.c,v 1.4 2024/11/01 21:11:37 riastradh Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #ifdef _LIBC
@@ -51,7 +51,7 @@ __weak_alias(strlcpy, _strlcpy)
  * Returns strlen(src); if retval >= siz, truncation occurred.
  */
 size_t
-strlcpy(char *dst, const char *src, size_t siz)
+strlcpy(char *__restrict dst, const char *__restrict src, size_t siz)
 {
 	char *d = dst;
 	const char *s = src;

Index: src/include/string.h
diff -u src/include/string.h:1.55 src/include/string.h:1.56
--- src/include/string.h:1.55	Fri Nov  1 16:36:58 2024
+++ src/include/string.h	Fri Nov  1 21:11:37 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: string.h,v 1.55 2024/11/01 16:36:58 nia Exp $	*/
+/*	$NetBSD: string.h,v 1.56 2024/11/01 21:11:37 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -45,67 +45,85 @@ typedef	_BSD_SIZE_T_	size_t;
 #include <sys/cdefs.h>
 #include <sys/featuretest.h>
 
+#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
+#  ifndef __LOCALE_T_DECLARED
+typedef struct _locale		*locale_t;
+#  define __LOCALE_T_DECLARED
+#  endif
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
+
 __BEGIN_DECLS
+#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
+void	*memccpy(void *, const void *, int, size_t);
+#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
 void	*memchr(const void *, int, size_t);
 int	 memcmp(const void *, const void *, size_t);
 void	*memcpy(void * __restrict, const void * __restrict, size_t);
+#if (_POSIX_C_SOURCE - 0 >= 202405L) || defined(_NETBSD_SOURCE)
+void	*memmem(const void *, size_t, const void *, size_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
 void	*memmove(void *, const void *, size_t);
 void	*memset(void *, int, size_t);
+#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
+char	*stpcpy(char * __restrict, const char * __restrict);
+char	*stpncpy(char * __restrict, const char * __restrict, size_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
 char	*strcat(char * __restrict, const char * __restrict);
 char	*strchr(const char *, int);
 int	 strcmp(const char *, const char *);
 int	 strcoll(const char *, const char *);
+#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
+int	 strcoll_l(const char *, const char *, locale_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
 char	*strcpy(char * __restrict, const char * __restrict);
 size_t	 strcspn(const char *, const char *);
+#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_XOPEN_SOURCE) || \
+    defined(_NETBSD_SOURCE)
+char	*strdup(const char *);
+#endif /* _POSIX_C_SOURCE || _XOPEN_SOURCE || _NETBSD_SOURCE */
 __aconst char *strerror(int);
+#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
+__aconst char *strerror_l(int, locale_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
+#if (_POSIX_C_SOURCE - 0 >= 200112L) || \
+    defined(_REENTRANT) || defined(_NETBSD_SOURCE)
+int	 strerror_r(int, char *, size_t);
+#endif /* _POSIX_C_SOURCE || _REENTRANT || _NETBSD_SOURCE */
+#if (_POSIX_C_SOURCE - 0 >= 202405L) || defined(_NETBSD_SOURCE)
+size_t	 strlcat(char * __restrict, const char * __restrict, size_t);
+size_t	 strlcpy(char * __restrict, const char * __restrict, size_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
 size_t	 strlen(const char *);
 char	*strncat(char * __restrict, const char * __restrict, size_t);
 int	 strncmp(const char *, const char *, size_t);
 char	*strncpy(char * __restrict, const char * __restrict, size_t);
-char	*strpbrk(const char *, const char *);
-char	*strrchr(const char *, int);
-size_t	 strspn(const char *, const char *);
-char	*strstr(const char *, const char *);
-char	*strtok(char * __restrict, const char * __restrict);
-#if (_POSIX_C_SOURCE - 0 >= 199506L) || (_XOPEN_SOURCE - 0 >= 500) || \
-    defined(_REENTRANT) || defined(_NETBSD_SOURCE)
-char	*strtok_r(char *, const char *, char **);
-int	 strerror_r(int, char *, size_t);
-#endif /* _POSIX_C_SOURCE >= 199506 || XOPEN_SOURCE >= 500 || ... */
-size_t	 strxfrm(char * __restrict, const char * __restrict, size_t);
-
-#if (_POSIX_C_SOURCE - 0 >= 200112L) || defined(_XOPEN_SOURCE) || \
-    defined(_NETBSD_SOURCE)
-void	*memccpy(void *, const void *, int, size_t);
-char	*strdup(const char *);
-#endif
-
-#if (_POSIX_C_SOURCE - 0 >= 200809L) || (_XOPEN_SOURCE - 0 >= 700) || \
-    defined(_NETBSD_SOURCE)
-char	*stpcpy(char * __restrict, const char * __restrict);
-char	*stpncpy(char * __restrict, const char * __restrict, size_t);
+#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
 char	*strndup(const char *, size_t);
 size_t	strnlen(const char *, size_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
+char	*strpbrk(const char *, const char *);
+char	*strrchr(const char *, int);
+#if (_POSIX_C_SOURCE - 0 >= 200809L) || defined(_NETBSD_SOURCE)
 #ifndef __STRSIGNAL_DECLARED
 #define __STRSIGNAL_DECLARED
 /* also in unistd.h */
 __aconst char *strsignal(int);
 #endif /* __STRSIGNAL_DECLARED */
 #endif
+size_t	 strspn(const char *, const char *);
+char	*strstr(const char *, const char *);
+char	*strtok(char * __restrict, const char * __restrict);
+#if (_POSIX_C_SOURCE - 0 >= 200112L) || \
+    defined(_REENTRANT) || defined(_NETBSD_SOURCE)
+char	*strtok_r(char *, const char *, char **);
+#endif /* _POSIX_C_SOURCE || _REENTRANT || _NETBSD_SOURCE */
+size_t	 strxfrm(char * __restrict, const char * __restrict, size_t);
+#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
+size_t	 strxfrm_l(char * __restrict, const char * __restrict, size_t,
+	    locale_t);
+#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
 __END_DECLS
 
-/*
- * IEEE Std 1003.1-2024 (POSIX.1-2024)
- */
-#if (_POSIX_C_SOURCE - 0) >= 202405L || (_XOPEN_SOURCE - 0 >= 800) || \
-    defined(_NETBSD_SOURCE)
-__BEGIN_DECLS
-void	*memmem(const void *, size_t, const void *, size_t);
-size_t	 strlcat(char *, const char *, size_t);
-size_t	 strlcpy(char *, const char *, size_t);
-__END_DECLS
-#endif
-
 #if defined(_NETBSD_SOURCE)
 #include <strings.h>		/* for backwards-compatibility */
 __BEGIN_DECLS
@@ -119,19 +137,7 @@ void	*mempcpy(void * __restrict, const v
 void	*explicit_memset(void *, int, size_t);
 int	consttime_memequal(const void *, const void *, size_t);
 __END_DECLS
-#endif
-
-#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
-#  ifndef __LOCALE_T_DECLARED
-typedef struct _locale		*locale_t;
-#  define __LOCALE_T_DECLARED
-#  endif
-__BEGIN_DECLS
-int	 strcoll_l(const char *, const char *, locale_t);
-size_t	 strxfrm_l(char * __restrict, const char * __restrict, size_t, locale_t);
-__aconst char *strerror_l(int, locale_t);
-__END_DECLS
-#endif /* _POSIX_C_SOURCE || _NETBSD_SOURCE */
+#endif /* _NETBSD_SOURCE */
 
 #if _FORTIFY_SOURCE > 0
 #include <ssp/string.h>

Index: src/sys/lib/libkern/libkern.h
diff -u src/sys/lib/libkern/libkern.h:1.146 src/sys/lib/libkern/libkern.h:1.147
--- src/sys/lib/libkern/libkern.h:1.146	Wed Oct  9 13:59:09 2024
+++ src/sys/lib/libkern/libkern.h	Fri Nov  1 21:11:37 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: libkern.h,v 1.146 2024/10/09 13:59:09 christos Exp $	*/
+/*	$NetBSD: libkern.h,v 1.147 2024/11/01 21:11:37 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -458,8 +458,8 @@ void	 mi_vector_hash(const void * __rest
 int	 scanc(u_int, const u_char *, const u_char *, int);
 int	 skpc(int, size_t, u_char *);
 int	 strcasecmp(const char *, const char *);
-size_t	 strlcpy(char *, const char *, size_t);
-size_t	 strlcat(char *, const char *, size_t);
+size_t	 strlcpy(char * __restrict, const char * __restrict, size_t);
+size_t	 strlcat(char * __restrict, const char * __restrict, size_t);
 int	 strncasecmp(const char *, const char *, size_t);
 u_long	 strtoul(const char *, char **, int);
 long long strtoll(const char *, char **, int);

Reply via email to