@@ -20,7 +20,10 @@ Major new features:
Deprecated and removed features, and other changes affecting compatibility:
- [Add deprecations, removals and changes affecting compatibility here]
+* For newly linked applications, the strtok function is now thread-safe.
+ The first call to strtok on each thread must have a non-null first
+ argument. (It is recommended that applications and libraries in
+ particular use strtok_r.)
Changes to build and runtime requirements:
@@ -204,6 +204,8 @@ tests := \
tst-strlcpy2 \
tst-strlen \
tst-strtok \
+ tst-strtok-compat \
+ tst-strtok-thread \
tst-strtok_r \
tst-strxfrm \
tst-strxfrm2 \
@@ -307,5 +309,7 @@ $(objpfx)bug-strcoll2.out: $(gen-locales)
$(objpfx)tst-strcoll-overflow.out: $(gen-locales)
$(objpfx)tst-strsignal.out: $(gen-locales)
$(objpfx)tst-strerror.out: $(gen-locales)
+$(objpfx)tst-strtok-compat.out: $(shared-thread-library)
+$(objpfx)tst-strtok-thread.out: $(shared-thread-library)
endif
@@ -96,4 +96,7 @@ libc {
strlcat;
strlcpy;
}
+ GLIBC_2.42 {
+ strtok;
+ }
}
@@ -16,7 +16,8 @@
<https://www.gnu.org/licenses/>. */
#include <string.h>
-
+#include <tls-internal.h>
+#include <shlib-compat.h>
/* Parse S into tokens separated by characters in DELIM.
If S is NULL, the last string strtok() was called with is
@@ -28,8 +29,24 @@
// s = "abc\0=-def\0"
*/
char *
-strtok (char *s, const char *delim)
+__strtok (char *s, const char *delim)
+{
+ struct tls_internal_t *tls_internal = __glibc_tls_internal ();
+ return __strtok_r (s, delim, &tls_internal->strtok_buf);
+}
+versioned_symbol (libc, __strtok, strtok, GLIBC_2_42);
+
+#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_42)
+/* The previous version was not thread-safe. */
+char *
+__strtok_old (char *s, const char *delim)
{
static char *olds;
+ if (olds == NULL && s == NULL)
+ /* Compatibility kludge for very old applications expecting BSD
+ behavior. */
+ return NULL;
return __strtok_r (s, delim, &olds);
}
+compat_symbol (libc, __strtok_old, strtok, GLIBC_2_0);
+#endif
new file mode 100644
@@ -0,0 +1,63 @@
+/* Verify BSD-compatible behavior of non-thread-safe strtok.
+ 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 <dlfcn.h>
+#include <first-versions.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/xthread.h>
+
+/* Compatibility symbol variant of strtok. */
+static __typeof (strtok) *old_impl;
+
+static void *
+thread_func (void *ignored)
+{
+ TEST_COMPARE_STRING (old_impl (NULL, ":"), "bb");
+ return NULL;
+}
+
+static int
+do_test (void)
+{
+ old_impl = dlvsym (RTLD_DEFAULT, "strtok", FIRST_VERSION_libc_strtok_STRING);
+ TEST_VERIFY (old_impl != NULL);
+
+ /* Check to see if there is a distinct compatibility symbol. */
+ if (old_impl == dlsym (RTLD_DEFAULT, "strtok"))
+ {
+ puts ("info: strtok is always thread-safe on this target");
+ return 0;
+ }
+
+ /* This tests the legacy BSD behavior. */
+ TEST_VERIFY (old_impl (NULL, ":") == NULL);
+
+ /* Verify that the old implementation is not thread-safe. */
+ char buf[] = "aa:bb:cc";
+ TEST_COMPARE_STRING (old_impl (buf, ":"), "aa");
+ xpthread_join (xpthread_create (NULL, thread_func, NULL));
+ TEST_COMPARE_STRING (old_impl (NULL, ":"), "cc");
+ TEST_COMPARE_STRING (old_impl (NULL, ":"), NULL);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
new file mode 100644
@@ -0,0 +1,50 @@
+/* Test that strtok uses a per-thread internal pointer.
+ 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 <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/xthread.h>
+
+static void *
+thread_func (void *ignored)
+{
+ char buf[] = "11,22,33";
+ TEST_COMPARE_STRING (strtok (buf, ","), "11");
+ TEST_COMPARE_STRING (strtok (NULL, ","), "22");
+ TEST_COMPARE_STRING (strtok (NULL, ","), "33");
+ TEST_COMPARE_STRING (strtok (NULL, ","), NULL);
+ return NULL;
+}
+
+static int
+do_test (void)
+{
+ /* Verify that the old implementation is not thread-safe. */
+ char buf[] = "aa:bb:cc";
+ TEST_COMPARE_STRING (strtok (buf, ":"), "aa");
+ xpthread_join (xpthread_create (NULL, thread_func, NULL));
+ TEST_COMPARE_STRING (strtok (NULL, ":"), "bb");
+ TEST_COMPARE_STRING (strtok (NULL, ":"), "cc");
+ TEST_COMPARE_STRING (strtok (NULL, ":"), NULL);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
@@ -23,6 +23,7 @@ struct tls_internal_t
{
char *strsignal_buf;
char *strerror_l_buf;
+ char *strtok_buf;
};
#endif
@@ -2614,6 +2614,7 @@ GLIBC_2.42 pthread_rwlockattr_destroy F
GLIBC_2.42 pthread_rwlockattr_getpshared F
GLIBC_2.42 pthread_rwlockattr_init F
GLIBC_2.42 pthread_rwlockattr_setpshared F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2297,6 +2297,7 @@ GLIBC_2.42 pthread_rwlockattr_destroy F
GLIBC_2.42 pthread_rwlockattr_getpshared F
GLIBC_2.42 pthread_rwlockattr_init F
GLIBC_2.42 pthread_rwlockattr_setpshared F
+GLIBC_2.42 strtok F
HURD_CTHREADS_0.3 __cthread_getspecific F
HURD_CTHREADS_0.3 __cthread_keycreate F
HURD_CTHREADS_0.3 __cthread_setspecific F
@@ -2753,3 +2753,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -3100,6 +3100,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2514,3 +2514,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2806,6 +2806,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2803,6 +2803,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2790,3 +2790,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2827,6 +2827,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -3010,6 +3010,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2274,3 +2274,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2786,6 +2786,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2953,6 +2953,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2839,3 +2839,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2836,3 +2836,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2914,6 +2914,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2912,6 +2912,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2920,6 +2920,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2822,6 +2822,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2264,3 +2264,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -3143,6 +3143,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -3188,6 +3188,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2897,6 +2897,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2973,3 +2973,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2517,3 +2517,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -2717,3 +2717,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
@@ -3141,6 +3141,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2934,6 +2934,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2833,6 +2833,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2830,6 +2830,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -3162,6 +3162,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2798,6 +2798,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2749,6 +2749,7 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F
GLIBC_2.5 __readlinkat_chk F
GLIBC_2.5 inet6_opt_append F
GLIBC_2.5 inet6_opt_find F
@@ -2768,3 +2768,4 @@ GLIBC_2.41 sched_setattr F
GLIBC_2.42 __inet_ntop_chk F
GLIBC_2.42 __inet_pton_chk F
GLIBC_2.42 pthread_gettid_np F
+GLIBC_2.42 strtok F