From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C80FC3858C1F for ; Mon, 7 Nov 2022 08:55:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C80FC3858C1F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667811355; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=j/ebnynEFLNade4rE7ovKdE+rX225n0TkLsdhBCP1Z0=; b=B6DBe8dOENbukNGHPaeMiPkm0FfyNfyQJZmZY3gl3FqnRks6AhgJRNUUvvrfRfxRu193fU qupBsshkIVEDGdnZVBqixW277CgMtOaI7xkP9HqRVteeDb1MSqLhUx3W2wzTLAfW4u8ksv otWAIxeM6xVElYAgXNApvINhH01AEDA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-613-y0zMQ16gPz6CaCHJSkLJzw-1; Mon, 07 Nov 2022 03:55:54 -0500 X-MC-Unique: y0zMQ16gPz6CaCHJSkLJzw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C4F603850E80 for ; Mon, 7 Nov 2022 08:55:53 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.86]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4278140C2064 for ; Mon, 7 Nov 2022 08:55:53 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] string: strtok, strtok_r should accept initial NULL subject (bug 16640) Date: Mon, 07 Nov 2022 09:55:51 +0100 Message-ID: <87bkpjm9d4.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 + . */ + +#include +#include +#include + +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 base-commit: 9cc9d61ee12f2f8620d8e0ea3c42af02bf07fe1e