From patchwork Wed Aug 14 23:35:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972624 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=cCcbTuZx; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wkl590Zqdz1yfL for ; Thu, 15 Aug 2024 09:36:29 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD1A23858C52 for ; Wed, 14 Aug 2024 23:36:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD1A23858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723678586; bh=8dvapLjorfqxRUrl2GkiBBwsDJBitvCzjKmax67BDP4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=cCcbTuZxi4p6ok0mDfUTUKRzZns2rUkH4I1Btlx/FPPfVyLe5HmsFuq0VqP7PCBLD nnt0kVrXDjcEiTUdY4VFgNnQbJD0gPnTiI+9pmOuTdYhqdiuA+pFvuwYvXCciRJUuC kxIx1nScMrid4tT5lh97+UlscvYi1fvXCt7LDnR8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from rusty.tulip.relay.mailchannels.net (rusty.tulip.relay.mailchannels.net [23.83.218.252]) by sourceware.org (Postfix) with ESMTPS id 165F83858D3C for ; Wed, 14 Aug 2024 23:35:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 165F83858D3C Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 165F83858D3C Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.218.252 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; cv=pass; b=odOO4D6QlqSgmhD4rbbRhTsd8rwee0Q80Nm0WF774tKM1GJ7pHb+KUEuOmTOiwCDHwsFj9Qsm5B1BLn95EU2xknzPQwYveVLkCqHs3dp2hmF7r6zLibuvcknYGsx5zn4slPpwxqMqPw2QzCvC+sdrgP5t7r3gyq6Lnp1jAUXuHk= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; c=relaxed/simple; bh=AQ42oEC6igwvVlZfuEG0EbMYGZWnZEV52Lywy6z4WXM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Da6/dznGsbqbXmVNlZifWafLoy7kSrcM7otgBTPSsA/aF8HyHJsMN9tPvcPeGz/UySMEfiNCrZRBDJq9FyVWFX54DgT7g2G3QGn/VFY+vCw3Y7VnAfc3/D6+IzTOCoMVtpxaV5NZKmLYZsRNDUP2bMY2QmNCG6n2eUBizrai83U= ARC-Authentication-Results: i=2; server2.sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 32D4D5C5168; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) Received: from pdx1-sub0-mail-a310.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id DA0525C5110; Wed, 14 Aug 2024 23:35:53 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723678553; a=rsa-sha256; cv=none; b=J/IMQ3Go18gdQz8bUT5Cm/tzo22pnwk41b7NnnCR1coj7LM9MuV180hR8BMovBQbXqse9Q A61qGxkJSxnrjVlK/DV6sjmtjwwHrzF6ega9P0DGzD/Fa4ncKpf3qzj387StOPoGlE3c6C 2C8NkvukxWQtkUWfwzlX4mYFsg0P9r6ZKv9azBi9mhoeeekG+uNIJR3vme1bSVFPx0T6qW wTYr77lLyH1b4J3FD6fXuaW4kvXzzryvy+8a5Xe16i5Vcff2Uz57jX/JD+aFcvXGPa4LF8 8+OyRg0zirS1uLMVRqQBbA7VXzon48QS2n31xUJbtrug0ECVHFeqmU6vEQD0Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723678553; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8dvapLjorfqxRUrl2GkiBBwsDJBitvCzjKmax67BDP4=; b=7EsnIuePfSk6fZjMDLiVfjLXQTwTDqI6SMGAFXjIFb8j4kh6Gwb2uI3kK/jduVNz1rY0rU 8SAHCB1stSIfbwVNR7lFx6BItpoRvt5e2tE5hKTeha8z3fNigBcanVZnD1bz83Ldj/cz7p w3H1KvH4VgnodJOYRUVjiiEVMakY6/tx5wQAgOrhMjCHzf7kenYRty3s0G8iYLPEdUV9rk bMXKQVXWWmdgieVTfC3bFcPGx+nNSzvVYKK/WSSvf293M80TbUWS64hrcGdng3AQNws/Ui e0nUFpHWGgX4SBVt2gFeso3x4lR72O5NwSKnP2U0Xdj6ntmZ4FiieIQ9hl8BHQ== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-vtfc5; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Callous-Tank: 730bbb4e280715b5_1723678554111_2636936558 X-MC-Loop-Signature: 1723678554111:4251515088 X-MC-Ingress-Time: 1723678554111 Received: from pdx1-sub0-mail-a310.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.121.240.13 (trex/7.0.2); Wed, 14 Aug 2024 23:35:54 +0000 Received: from fedora.redhat.com (unknown [184.146.171.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a310.dreamhost.com (Postfix) with ESMTPSA id 4Wkl4T3R0pz9p; Wed, 14 Aug 2024 16:35:53 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: fweimer@redhat.com Subject: [PATCH 1/3] Make tst-ungetc use libsupport Date: Wed, 14 Aug 2024 19:35:32 -0400 Message-ID: <20240814233534.1469084-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240814233534.1469084-1-siddhesh@sourceware.org> References: <20240814233534.1469084-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1170.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Signed-off-by: Siddhesh Poyarekar Reviewed-by: Carlos O'Donell --- stdio-common/tst-ungetc.c | 112 +++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/stdio-common/tst-ungetc.c b/stdio-common/tst-ungetc.c index 1344b2b591..5c808f0734 100644 --- a/stdio-common/tst-ungetc.c +++ b/stdio-common/tst-ungetc.c @@ -1,70 +1,72 @@ -/* Test for ungetc bugs. */ +/* Test for ungetc bugs. + Copyright (C) 1996-2024 Free Software Foundation, Inc. + Copyright The GNU Toolchain Authors. + 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 - -#undef assert -#define assert(x) \ - if (!(x)) \ - { \ - fputs ("test failed: " #x "\n", stderr); \ - retval = 1; \ - goto the_end; \ - } +#include +#include +#include +#include +#include -int -main (int argc, char *argv[]) +static int +do_test (void) { - char name[] = "/tmp/tst-ungetc.XXXXXX"; + char *name = NULL; FILE *fp = NULL; - int retval = 0; int c; char buffer[64]; - int fd = mkstemp (name); + int fd = create_temp_file ("tst-ungetc.", &name); if (fd == -1) - { - printf ("mkstemp failed: %m\n"); - return 1; - } - close (fd); - fp = fopen (name, "w"); - assert (fp != NULL) - fputs ("bla", fp); - fclose (fp); - fp = NULL; + FAIL_EXIT1 ("cannot create temporary file: %m"); + xclose (fd); - fp = fopen (name, "r"); - assert (fp != NULL); - assert (ungetc ('z', fp) == 'z'); - assert (getc (fp) == 'z'); - assert (getc (fp) == 'b'); - assert (getc (fp) == 'l'); - assert (ungetc ('m', fp) == 'm'); - assert (getc (fp) == 'm'); - assert ((c = getc (fp)) == 'a'); - assert (getc (fp) == EOF); - assert (ungetc (c, fp) == c); - assert (feof (fp) == 0); - assert (getc (fp) == c); - assert (getc (fp) == EOF); - fclose (fp); - fp = NULL; + fp = xfopen (name, "w"); + fputs ("bla", fp); + xfclose (fp); - fp = fopen (name, "r"); - assert (fp != NULL); - assert (getc (fp) == 'b'); - assert (getc (fp) == 'l'); - assert (ungetc ('b', fp) == 'b'); - assert (fread (buffer, 1, 64, fp) == 2); - assert (buffer[0] == 'b'); - assert (buffer[1] == 'a'); + fp = xfopen (name, "r"); + TEST_VERIFY_EXIT (ungetc ('z', fp) == 'z'); + TEST_VERIFY_EXIT (getc (fp) == 'z'); + TEST_VERIFY_EXIT (getc (fp) == 'b'); + TEST_VERIFY_EXIT (getc (fp) == 'l'); + TEST_VERIFY_EXIT (ungetc ('m', fp) == 'm'); + TEST_VERIFY_EXIT (getc (fp) == 'm'); + TEST_VERIFY_EXIT ((c = getc (fp)) == 'a'); + TEST_VERIFY_EXIT (getc (fp) == EOF); + TEST_VERIFY_EXIT (ungetc (c, fp) == c); + TEST_VERIFY_EXIT (feof (fp) == 0); + TEST_VERIFY_EXIT (getc (fp) == c); + TEST_VERIFY_EXIT (getc (fp) == EOF); + xfclose (fp); -the_end: - if (fp != NULL) - fclose (fp); - unlink (name); + fp = xfopen (name, "r"); + TEST_VERIFY_EXIT (getc (fp) == 'b'); + TEST_VERIFY_EXIT (getc (fp) == 'l'); + TEST_VERIFY_EXIT (ungetc ('b', fp) == 'b'); + TEST_VERIFY_EXIT (fread (buffer, 1, 64, fp) == 2); + TEST_VERIFY_EXIT (buffer[0] == 'b'); + TEST_VERIFY_EXIT (buffer[1] == 'a'); + xfclose (fp); - return retval; + return 0; } + +#include From patchwork Wed Aug 14 23:35:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972623 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=SHSRLbMv; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wkl585yTcz1yNr for ; Thu, 15 Aug 2024 09:36:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A2B243858403 for ; Wed, 14 Aug 2024 23:36:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A2B243858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723678586; bh=wNnSg2WhErcIbYptrMgpMjTRR2HBd/MF6YGL3oHEWaI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=SHSRLbMv5kqJJp3gAQSpRbqg8lbpKEVc537vKnQOkNwGRtZFmmK+G/0d1SP5PLICH 8lhkF4CJmkc9mysIi6+DWzM7y61VpO4NQddtZmttNDCpgJeJ8MjqDcIonpNYteud3s BuGSbyiztP5CtgG6j+5q6R2RbGw65SzgloWlwHnk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from elephant.ash.relay.mailchannels.net (elephant.ash.relay.mailchannels.net [23.83.222.57]) by sourceware.org (Postfix) with ESMTPS id 9C65A3858C50 for ; Wed, 14 Aug 2024 23:35:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C65A3858C50 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9C65A3858C50 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.222.57 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; cv=pass; b=DYWvjzEMWdOJEn1AkhewkLqxBw+vVFILIeofy5NjJVfKYOtQ3lSfIVu6CmPrd8LWYSIYjIvsUY4o5jlNo+4w2I5mqLimCgYosDrZ+oi3HXwxNrtkZZZt//knlcOGMXxylduQHq2YpYGUk+nizjS7ym9YUIY+S9LBpTaljRPbOVQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; c=relaxed/simple; bh=hE6Fub/GTVwxpPl+wyFbQ20VKALGqovCfYcDcvS55VU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=N2jQfwu1j/Uct4l+PVQb1nkrwB3dbiPdH0nfS5ZizAzkxNsBieyqIfNDUkPbpsZPeFqv657DJgm5bXQrAHtcV4iZToS1viDzebov1UvLq9wRCFcfhMt9pVss1VnhrLVLlxh+4yIgHjgouiL/1m780Q5YaniqZAihW7vkDM3nF0g= ARC-Authentication-Results: i=2; server2.sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 8055C846B66; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) Received: from pdx1-sub0-mail-a310.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 372F7846A98; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723678554; a=rsa-sha256; cv=none; b=myhKPPBNcJknl61xyioF5oAjbYx67AMkJ5wcM2YzBOBuJVz/z2DROQhL84S9MD9lW/91Av hOxJ6LsMiVsqpY6FCr9Ri814kIOK9WRP/loxVpn662+1MQpyZTunSYRw9ytCOvAmx7rb4u YgsD4agZDyihqnhWBtgBBdSykaPXsSuEpy2WE1RTzuDyVu7p1dbXt3keFrnlN50KunhlCH QfDZPXPY75mz46EN/ef/VfHIlVfgOAFdP8PTSWYVZ8iwWN6PakKbXj2q85cD5JnQT8q8ht sMTeJs1PQpOPQ2kjnOcqhCackFlccMV0+6NyPz3zzaJ04SCBl1rp9BGfPFlM8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723678554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wNnSg2WhErcIbYptrMgpMjTRR2HBd/MF6YGL3oHEWaI=; b=xfWzIQVe5DkAGksfm0jwTw9L+J6OmaCx9TZzPf/pHV4kSYss88Qdajq8TYN1mZMiKYQMKZ w/cUxvmBTAu23ZR61CP/NHi4IiGtQYnHdmGrM7uw9h0/P/r8LGHxoyYhniz7VXCHOIE9eu qapF6k5CPCALMwqVBLT+M4x/1jJRCyzopEPpygxP0bPreuvtlTGfYt1rTGsKCGn62BJLnG y79oeQAh4XTrLuV/ruW/EaboLF70bxWf+vre7xMpEaII4yS0EaYKmBCxLJ0fGVEH2fQ0Ch d1AMQm960CQHnEjkmDKh3+BSzRPFv08UV5CC9VhkY2x9uVCzj+WiXR+7l5hOEQ== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-rb4wz; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Cooing-Army: 19abb605254625ae_1723678554435_64807049 X-MC-Loop-Signature: 1723678554435:1328571160 X-MC-Ingress-Time: 1723678554435 Received: from pdx1-sub0-mail-a310.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.121.240.13 (trex/7.0.2); Wed, 14 Aug 2024 23:35:54 +0000 Received: from fedora.redhat.com (unknown [184.146.171.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a310.dreamhost.com (Postfix) with ESMTPSA id 4Wkl4T6KbJzM2; Wed, 14 Aug 2024 16:35:53 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: fweimer@redhat.com Subject: [PATCH 2/3] ungetc: Fix uninitialized read when putting into unused streams [BZ #27821] Date: Wed, 14 Aug 2024 19:35:33 -0400 Message-ID: <20240814233534.1469084-3-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240814233534.1469084-1-siddhesh@sourceware.org> References: <20240814233534.1469084-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1171.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org When ungetc is called on an unused stream, the backup buffer is allocated without the main get area being present. This results in two every subsequent ungetc (as the stream remains in the backup area) checking uninitialized memory in the backup buffer when trying to put a character back into the stream. Avoid comparing the input character with buffer contents when in backup to avoid this uninitialized read. The uninitialized read is harmless in this context since the location is promptly overwritten with the input character, thus fulfilling ungetc functionality. Also adjust wording in the manual to drop the paragraph that says glibc cannot do multiple ungetc back to back since with this change, ungetc can actually do this. Signed-off-by: Siddhesh Poyarekar Reviewed-by: Carlos O'Donell --- libio/genops.c | 2 +- manual/stdio.texi | 8 +++----- stdio-common/tst-ungetc.c | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libio/genops.c b/libio/genops.c index 99f5e80f20..b012fa33d2 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -662,7 +662,7 @@ _IO_sputbackc (FILE *fp, int c) { int result; - if (fp->_IO_read_ptr > fp->_IO_read_base + if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp) && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c) { fp->_IO_read_ptr--; diff --git a/manual/stdio.texi b/manual/stdio.texi index 8517653507..92614775fa 100644 --- a/manual/stdio.texi +++ b/manual/stdio.texi @@ -1467,11 +1467,9 @@ program; usually @code{ungetc} is used only to unread a character that was just read from the same stream. @Theglibc{} supports this even on files opened in binary mode, but other systems might not. -@Theglibc{} only supports one character of pushback---in other -words, it does not work to call @code{ungetc} twice without doing input -in between. Other systems might let you push back multiple characters; -then reading from the stream retrieves the characters in the reverse -order that they were pushed. +@Theglibc{} supports pushing back multiple characters; subsequently +reading from the stream retrieves the characters in the reverse order +that they were pushed. Pushing back characters doesn't alter the file; only the internal buffering for the stream is affected. If a file positioning function diff --git a/stdio-common/tst-ungetc.c b/stdio-common/tst-ungetc.c index 5c808f0734..388b202493 100644 --- a/stdio-common/tst-ungetc.c +++ b/stdio-common/tst-ungetc.c @@ -48,6 +48,8 @@ do_test (void) TEST_VERIFY_EXIT (getc (fp) == 'b'); TEST_VERIFY_EXIT (getc (fp) == 'l'); TEST_VERIFY_EXIT (ungetc ('m', fp) == 'm'); + TEST_VERIFY_EXIT (ungetc ('n', fp) == 'n'); + TEST_VERIFY_EXIT (getc (fp) == 'n'); TEST_VERIFY_EXIT (getc (fp) == 'm'); TEST_VERIFY_EXIT ((c = getc (fp)) == 'a'); TEST_VERIFY_EXIT (getc (fp) == EOF); From patchwork Wed Aug 14 23:35:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972625 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=g3ZbIbXy; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wkl6T5BL4z1yNr for ; Thu, 15 Aug 2024 09:37:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E94B3858433 for ; Wed, 14 Aug 2024 23:37:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E94B3858433 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723678655; bh=NslUCYr9Fcu5LyO98ansPTVI5zix8Nh61LZFribpSrA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=g3ZbIbXyYH51bGZ7p3eKRWaQaQbf3+vUUmlE1NpQ/BiQd90KucsCQRcDgq4HC3BhB Dj8VtXSNNxKbl62C+gZFxKsJGaPd/1/+pVnYf4uFNMgVVOGozzbwmhPnVmCeuRItQn 1oQddWKCfA7GydUFfL7jnvX7dO13OUFSvKbrIbQU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from quail.birch.relay.mailchannels.net (quail.birch.relay.mailchannels.net [23.83.209.151]) by sourceware.org (Postfix) with ESMTPS id 0D84C3858C52 for ; Wed, 14 Aug 2024 23:35:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0D84C3858C52 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0D84C3858C52 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.209.151 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; cv=pass; b=InAaE/fg1st6hmmHRby6j9ylOHaamY9XrGIJa/D2SEkM7zG9ytYOMsXj7DNIHjGH7ag6ArERmTUH47zbnHNHDTpV9KZffLSIJoiWyS0RFSOIr9+4y53R2MToG7/v8QBw6cpfaPQzPbM0jEzjwySxOHjMUxvH3L27hkkqGC6pkP0= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; c=relaxed/simple; bh=gRLArS50VyIJwJBZqjJkbhlusBuk5/SBj5JIB4fJBVQ=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=WwLNsXl4yWOXW8Im4wP7rTfYx63m4LVdFgpgh/Xi6l7vUd0F3sDEKxPga+NCH0DpCvwVKRBgdDvirlpuh4PJIIzrw1c3Wzk3btX+9zrlniNRkFpVMdSCjd7gE3PX7HDkIS0MvvT6eU8iZnwweuThBwTdEoPNq5NitaV9hj6J8zM= ARC-Authentication-Results: i=2; server2.sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id EC4CC9036D4; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) Received: from pdx1-sub0-mail-a310.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 9F96F9060E6; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723678554; a=rsa-sha256; cv=none; b=xa56NJsEvpMgeev+e0LvEXf3U7ymcGzMWqAglqliw3jWyNdhYNVbotYW5PwNmevolxaV9m AubUMvIE6fyhDfcgDJmGnYSheUb3FHVFIrrG7sDF5app5P4kSX3vFBTCirXu9tfe04qsmT t8wVpD3/s+XGGHLEyt37iX7iIakj7bG1qD1anRVO8e/S95k5QAP8aYYMsCsdD86w050V+7 NjePNz0tI4h6bQbQmEeJLIFxhsTa8SCcS+sCfteoh9hNVPlZfn5wLF+Vus9SfRNdgM/tql b5NoBGRMmZvOVrzPRwU5eBwRe+XkUozS+smqmy8KgScWnjWuAbVFN4x9o6i74Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723678554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NslUCYr9Fcu5LyO98ansPTVI5zix8Nh61LZFribpSrA=; b=aJn2VlLIRI6jpog4l/Zsty7zC1y8/ETwSXk5vwBY0oqEUK6Rwi/Z6ejp6VyR0m1Y6jRdft cpVcQVnRojxKK9quUo9JtQsVtYb4zFWkKUKO1KTdHEAsKOH7aldjzpuQN4Gi9A2NIWwNhg gK3NGb2unyQkXq/2ZVWR83kjD6rsmupG3RkP+Ro7YqBgRTfFPjDZ0sDvHKGTBhnAXNMYCd MTFUIFS+owR3TIRw/uFkUo/4oRJvZUFwELXU/Zp5KQQ+zWBK7Vf2O7rpa9T3b8SjhXXHHQ GDPqdg8jwvQBOOku/4Lw5ZRbvFeOFyLOObjzOmdR3BNM+uiPSapiP1BGwnblgQ== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-t6js8; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Irritate-Cold: 692b7ef10e25a8f8_1723678554871_2927200038 X-MC-Loop-Signature: 1723678554871:3670560241 X-MC-Ingress-Time: 1723678554871 Received: from pdx1-sub0-mail-a310.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.82.141 (trex/7.0.2); Wed, 14 Aug 2024 23:35:54 +0000 Received: from fedora.redhat.com (unknown [184.146.171.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a310.dreamhost.com (Postfix) with ESMTPSA id 4Wkl4V1vpwz9p; Wed, 14 Aug 2024 16:35:54 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: fweimer@redhat.com Subject: [PATCH 3/3] ungetc: Fix backup buffer leak on program exit [BZ #27821] Date: Wed, 14 Aug 2024 19:35:34 -0400 Message-ID: <20240814233534.1469084-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240814233534.1469084-1-siddhesh@sourceware.org> References: <20240814233534.1469084-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1171.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org If a file descriptor is left unclosed and is cleaned up by _IO_cleanup on exit, its backup buffer remains unfreed, registering as a leak in valgrind. This is not strictly an issue since (1) the program should ideally be closing the stream once it's not in use and (2) the program is about to exit anyway, so keeping the backup buffer around a wee bit longer isn't a real problem. Free it anyway to keep valgrind happy when the streams in question are the standard ones, i.e. stdout, stdin or stderr. Also, the _IO_have_backup macro checks for _IO_save_base, which is a roundabout way to check for a backup buffer instead of directly looking for _IO_backup_base. The roundabout check breaks when the main get area has not been used and user pushes a char into the backup buffer with ungetc. Fix this to use the _IO_backup_base directly. Signed-off-by: Siddhesh Poyarekar Reviewed-by: Carlos O'Donell --- libio/genops.c | 6 ++++++ libio/libioP.h | 4 ++-- stdio-common/Makefile | 7 +++++++ stdio-common/tst-ungetc-leak.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 stdio-common/tst-ungetc-leak.c diff --git a/libio/genops.c b/libio/genops.c index b012fa33d2..6ea95c5e68 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -816,6 +816,12 @@ _IO_unbuffer_all (void) legacy = 1; #endif + /* Free up the backup area if it ever was ever allocated. */ + if (_IO_have_backup (fp)) + _IO_free_backup_area (fp); + if (fp->_mode > 0 && _IO_have_wbackup (fp)) + _IO_free_wbackup_area (fp); + if (! (fp->_flags & _IO_UNBUFFERED) /* Iff stream is un-orientated, it wasn't used. */ && (legacy || fp->_mode != 0)) diff --git a/libio/libioP.h b/libio/libioP.h index 1af287b19f..616253fcd0 100644 --- a/libio/libioP.h +++ b/libio/libioP.h @@ -577,8 +577,8 @@ extern void _IO_old_init (FILE *fp, int flags) __THROW; ((__fp)->_wide_data->_IO_write_base \ = (__fp)->_wide_data->_IO_write_ptr = __p, \ (__fp)->_wide_data->_IO_write_end = (__ep)) -#define _IO_have_backup(fp) ((fp)->_IO_save_base != NULL) -#define _IO_have_wbackup(fp) ((fp)->_wide_data->_IO_save_base != NULL) +#define _IO_have_backup(fp) ((fp)->_IO_backup_base != NULL) +#define _IO_have_wbackup(fp) ((fp)->_wide_data->_IO_backup_base != NULL) #define _IO_in_backup(fp) ((fp)->_flags & _IO_IN_BACKUP) #define _IO_have_markers(fp) ((fp)->_markers != NULL) #define _IO_blen(fp) ((fp)->_IO_buf_end - (fp)->_IO_buf_base) diff --git a/stdio-common/Makefile b/stdio-common/Makefile index e4f0146d2c..a91754f52d 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -254,6 +254,7 @@ tests := \ tst-swscanf \ tst-tmpnam \ tst-ungetc \ + tst-ungetc-leak \ tst-unlockedio \ tst-vfprintf-mbs-prec \ tst-vfprintf-user-type \ @@ -316,6 +317,7 @@ tests-special += \ $(objpfx)tst-printf-bz25691-mem.out \ $(objpfx)tst-printf-fp-free-mem.out \ $(objpfx)tst-printf-fp-leak-mem.out \ + $(objpfx)tst-ungetc-leak-mem.out \ $(objpfx)tst-vfprintf-width-prec-mem.out \ # tests-special @@ -330,6 +332,8 @@ generated += \ tst-printf-fp-leak-mem.out \ tst-printf-fp-leak.mtrace \ tst-scanf-bz27650.mtrace \ + tst-ungetc-leak-mem.out \ + tst-ungetc-leak.mtrace \ tst-vfprintf-width-prec-mem.out \ tst-vfprintf-width-prec.mtrace \ # generated @@ -424,6 +428,9 @@ tst-printf-fp-leak-ENV = \ tst-scanf-bz27650-ENV = \ MALLOC_TRACE=$(objpfx)tst-scanf-bz27650.mtrace \ LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +tst-ungetc-leak-ENV = \ + MALLOC_TRACE=$(objpfx)tst-ungetc-leak.mtrace \ + LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so $(objpfx)tst-unbputc.out: tst-unbputc.sh $(objpfx)tst-unbputc $(SHELL) $< $(common-objpfx) '$(test-program-prefix)'; \ diff --git a/stdio-common/tst-ungetc-leak.c b/stdio-common/tst-ungetc-leak.c new file mode 100644 index 0000000000..6c5152b43f --- /dev/null +++ b/stdio-common/tst-ungetc-leak.c @@ -0,0 +1,32 @@ +/* Test for memory leak with ungetc when stream is unused. + Copyright The GNU Toolchain Authors. + 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 +#include + +static int +do_test (void) +{ + mtrace (); + TEST_COMPARE (ungetc('y', stdin), 'y'); + return 0; +} + +#include