From patchwork Mon Nov 7 08:55:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 60076 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 898213857BB6 for ; Mon, 7 Nov 2022 08:56:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 898213857BB6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667811379; bh=j/ebnynEFLNade4rE7ovKdE+rX225n0TkLsdhBCP1Z0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=i8nbV43AobMqnZgh3p9C9ahAKcEtXGLgj0EkhQQeQbq8jezzSVkX+C2w3CdWvyEBX c2YIejehur423mvd6DDhU5RJuqbGYP4SSWl3Rvm1WL8DyO0s2nNBEuxau9C9iRWyO3 Ga9S6Ep9Ynd8RciZpf7gfz9hGdaw5CQwJvXdIBns= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 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) 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 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "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(-) base-commit: 9cc9d61ee12f2f8620d8e0ea3c42af02bf07fe1e 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