From patchwork Wed Dec 21 09:29:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 62219 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 7CAE73858022 for ; Wed, 21 Dec 2022 09:30:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CAE73858022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671615012; bh=+JX/2xGCohjmqVcoYiZ2k2EFZrJIK7qm1kSjJ0xauYs=; h=To:Cc:Subject:References:Date:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=q2SZ15PmAI11m5MTm6EHmUK3ezCGEc8ULFIbSjbqu7D3keogLMLbAGr+XsA38UJHU XVKR+piC32+FnwJP6KNL7Ojcv7uX11PWq5lwWtTbbwgdFoAqH0vVsYDSt5HN3a9vVW HyLcz4VhezE05MtqGeEdDOzPhidfibbDpF78AWjs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 17931385781A for ; Wed, 21 Dec 2022 09:29:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 17931385781A Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 466816B429; Wed, 21 Dec 2022 09:29:41 +0000 (UTC) Received: from hawking.suse.de (unknown [10.168.4.11]) by relay2.suse.de (Postfix) with ESMTP id 4064B2C141; Wed, 21 Dec 2022 09:29:41 +0000 (UTC) Received: by hawking.suse.de (Postfix, from userid 17005) id 1BEB5444C6A; Wed, 21 Dec 2022 10:29:41 +0100 (CET) To: Carlos O'Donell Cc: libc-alpha@sourceware.org Subject: [PATCH] getdelim: ensure error indicator is set on error (bug 29917) References: <60addddd-0c0f-a0e5-f2ad-de7e7a12a854@redhat.com> <27d0eba1-3c9a-75b6-4681-bafbf13b5e31@redhat.com> X-Yow: I HAVE to buy a new ``DODGE MISER'' and two dozen JORDACHE JEANS because my viewscreen is ``USER-FRIENDLY''!! Date: Wed, 21 Dec 2022 10:29:41 +0100 In-Reply-To: <27d0eba1-3c9a-75b6-4681-bafbf13b5e31@redhat.com> (Carlos O'Donell's message of "Mon, 19 Dec 2022 12:47:56 -0500") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, 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: Andreas Schwab via Libc-alpha From: Andreas Schwab Reply-To: Andreas Schwab Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" POSIX requires that getdelim and getline set the error indicator on the stream when an error occured, in addition to setting errno. Reviewed-by: Carlos O'Donell --- libio/Makefile | 2 +- libio/iogetdelim.c | 16 +++++++++++----- libio/tst-getdelim.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 libio/tst-getdelim.c diff --git a/libio/Makefile b/libio/Makefile index 64398ab1ee..9c69a85c87 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -66,7 +66,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \ tst-fwrite-error tst-ftell-partial-wide tst-ftell-active-handler \ tst-ftell-append tst-fputws tst-bz22415 tst-fgetc-after-eof \ tst-sprintf-ub tst-sprintf-chk-ub tst-bz24051 tst-bz24153 \ - tst-wfile-sync tst-bz28828 + tst-wfile-sync tst-bz28828 tst-getdelim tests-internal = tst-vtables tst-vtables-interposed diff --git a/libio/iogetdelim.c b/libio/iogetdelim.c index b6c4c07b45..591526e9c1 100644 --- a/libio/iogetdelim.c +++ b/libio/iogetdelim.c @@ -43,11 +43,6 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) ssize_t cur_len = 0; ssize_t len; - if (lineptr == NULL || n == NULL) - { - __set_errno (EINVAL); - return -1; - } CHECK_FILE (fp, -1); _IO_acquire_lock (fp); if (_IO_ferror_unlocked (fp)) @@ -56,12 +51,21 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) goto unlock_return; } + if (lineptr == NULL || n == NULL) + { + __set_errno (EINVAL); + fseterr_unlocked (fp); + result = -1; + goto unlock_return; + } + if (*lineptr == NULL || *n == 0) { *n = 120; *lineptr = (char *) malloc (*n); if (*lineptr == NULL) { + fseterr_unlocked (fp); result = -1; goto unlock_return; } @@ -88,6 +92,7 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) if (__glibc_unlikely (len >= SSIZE_MAX - cur_len)) { __set_errno (EOVERFLOW); + fseterr_unlocked (fp); result = -1; goto unlock_return; } @@ -102,6 +107,7 @@ __getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) new_lineptr = (char *) realloc (*lineptr, needed); if (new_lineptr == NULL) { + fseterr_unlocked (fp); result = -1; goto unlock_return; } diff --git a/libio/tst-getdelim.c b/libio/tst-getdelim.c new file mode 100644 index 0000000000..1def193e8d --- /dev/null +++ b/libio/tst-getdelim.c @@ -0,0 +1,36 @@ +/* Check that getdelim sets error indicator on error (BZ #29917) + + 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) +{ + clearerr (stdin); + TEST_VERIFY (getdelim (0, 0, '\n', stdin) == -1); + TEST_VERIFY (ferror (stdin) != 0); + TEST_VERIFY (errno == EINVAL); + + return 0; +} + +#include