public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] string: strtok, strtok_r should accept initial NULL subject (bug 16640)
@ 2022-11-07  8:55 Florian Weimer
  2022-11-08 13:48 ` Carlos O'Donell
  0 siblings, 1 reply; 2+ messages in thread
From: Florian Weimer @ 2022-11-07  8:55 UTC (permalink / raw)
  To: libc-alpha

The BSD and musl implementations accept an initial NULL subject
argument.  This also used to be support in glibc on some architectures
with custom assembler code.

Tested on i686-linux-gnu and x86_64-linux-gnu.

---
 string/Makefile          |  1 +
 string/strtok_r.c        |  6 +++++-
 string/tst-strtok-null.c | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/string/Makefile b/string/Makefile
index 938f528b8d..a1c6587376 100644
--- a/string/Makefile
+++ b/string/Makefile
@@ -177,6 +177,7 @@ tests := \
   tst-strfry \
   tst-strlen \
   tst-strtok \
+  tst-strtok-null \
   tst-strtok_r \
   tst-strxfrm \
   tst-strxfrm2 \
diff --git a/string/strtok_r.c b/string/strtok_r.c
index fd3a842c99..8342d2ac74 100644
--- a/string/strtok_r.c
+++ b/string/strtok_r.c
@@ -44,7 +44,11 @@ __strtok_r (char *s, const char *delim, char **save_ptr)
   char *end;
 
   if (s == NULL)
-    s = *save_ptr;
+    {
+      if (*save_ptr == NULL)
+	return NULL;
+      s = *save_ptr;
+    }
 
   if (*s == '\0')
     {
diff --git a/string/tst-strtok-null.c b/string/tst-strtok-null.c
new file mode 100644
index 0000000000..2cbc4e5fc4
--- /dev/null
+++ b/string/tst-strtok-null.c
@@ -0,0 +1,32 @@
+/* Check that strtok and strtok_r accept NULL for the initial subject string.
+   Copyright (C) 2022 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 <string.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  TEST_COMPARE_STRING (strtok (NULL, ","), NULL);
+  char *save = NULL;
+  TEST_COMPARE_STRING (strtok_r (NULL, ",", &save), NULL);
+  return 0;
+}
+
+#include <support/test-driver.c>

base-commit: 9cc9d61ee12f2f8620d8e0ea3c42af02bf07fe1e


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-08 13:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-07  8:55 [PATCH] string: strtok, strtok_r should accept initial NULL subject (bug 16640) Florian Weimer
2022-11-08 13:48 ` Carlos O'Donell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).