From patchwork Thu Aug 15 18:00:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972868 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=sYjnMMfK; 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 4WlCc85P0nz1yYl for ; Fri, 16 Aug 2024 04:01:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8CA8A3857000 for ; Thu, 15 Aug 2024 18:01:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8CA8A3857000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723744886; bh=2Pldd4KI3CprnsRIMP2uL1TI2YID6LsD4rhME3Jarnc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=sYjnMMfKVy6aAOazTqVJpUlPuhDyAgwW/xSZcPgT+5C/CeDyXmPa/HjbovamzTekW Yk7EfMv2ywaU/J/1Ceio/q2A++d46vPNN00uFV3S6fB7SMcWBAQtdj2gb29QQG1hLs IgJzHNi0ru6sEYXH2gBEU7i/PWnLE9vwGIq1DgCI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cheetah.ash.relay.mailchannels.net (cheetah.ash.relay.mailchannels.net [23.83.222.34]) by sourceware.org (Postfix) with ESMTPS id 42F573858C41 for ; Thu, 15 Aug 2024 18:00:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42F573858C41 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 42F573858C41 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.222.34 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723744857; cv=pass; b=qWJsnuKg/uQeiybRcsX61xQt0ChuX4PFswDcEz+2iPayX2rCIQnnWZcSpM/+I04ijuAcDFU2sHM+6csCxlkFQTO2ZBfp2G34fitBwrI2sMhtrYzRejYlLj0U97XwqkPLZN6nlIpNIXCgCH4CRBJO8O0/odnTmMqvoJr3W11h03g= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723744857; c=relaxed/simple; bh=iOkLxsCfeRfJZe2xkv41uIBtUVT4PS9fcfzPF+ju+y0=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=XAV4XWvWu+ZdZ4EtKh8JiXMhXJVJ8GU5cezQkmaLIS1fZk0jni2PEGfde1AEcSgHPUMCag/D4yb7jL7q9JAT8EmOKn+R8wvpuEj+knjauNvJyAyE+iuC5hDcB68FyvnCMUSxydGX48NX6CyVUaxqiB3071MCBFu4ROJbDosbgDc= 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 02A7CC66A1; Thu, 15 Aug 2024 18:00:54 +0000 (UTC) Received: from pdx1-sub0-mail-a246.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 840A0C5569; Thu, 15 Aug 2024 18:00:53 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723744853; a=rsa-sha256; cv=none; b=Xv7dt+5ZhgTk9ezBeFO/gSTW4TT6dKuP+2L95Q5vqD5Wh3ZGdN/iJUa3ziLzZGJCv+Xrm/ oImrzWUwUPj4mQ7k1ebsDg2QwgCXZSpQjRPUr+OY0lMS/2fnQkO8qQun6LrzTovYTEMPia vf8ojxVVnJXa16ORzxj35VQaU6c+B3iMKEqiWaherRTrpzwS8pOUixVndM26FEF3Y8Ewhv aDdr7Gn28hKI7fsXnZjCVHl+oL4Lav9aTrvGLa6MUic1ivhrMmP4nQz+QK0Lh7IVwJXRqP 4Y2qrv3rZwNOmGOlYdXtJy9jmr7W/bIQU/rtt4Np+pR52PT+1rRRjOnrpumxrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723744853; 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=2Pldd4KI3CprnsRIMP2uL1TI2YID6LsD4rhME3Jarnc=; b=fHAlSG6+kAT8nm0AiUdBy46iOVUKlzwI+IrL3A22C8T6Xfdv3+EQap3ei8QaY1BAmOjmkY b6qgrix0pgtOAze5jYou+TQs5XU4ZGCEPLcsExueTXUw/wBWsvfqHMMblmYYc/QEBdsyY2 GsNfu++Vj/ioakq+Pz5JUhCH0wieOHQ/av1PFrCLW4MaK4KWknnw0ZsV6dqyd5HUNUgKdX ld8BYyUY4jBGwO8GMQnEpMwfPZghtmK0c++HJ3COwxUwAqUh86DmMvF3Nwjr9gaM1TcuT6 CspEa5rUVO1wu+1crUZKq2zAV7fne34SRykojLmHsTJ6ncz12N+xAhKKIQXyrg== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-ztbxp; 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-Rock-Wide-Eyed: 44db4b9d3cfc5e21_1723744853759_3732005932 X-MC-Loop-Signature: 1723744853759:2310480691 X-MC-Ingress-Time: 1723744853759 Received: from pdx1-sub0-mail-a246.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.124.208.81 (trex/7.0.2); Thu, 15 Aug 2024 18:00:53 +0000 Received: from fedora.redhat.com (bras-base-toroon4859w-grc-84-184-146-171-51.dsl.bell.ca [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-a246.dreamhost.com (Postfix) with ESMTPSA id 4WlCbT1FdyzHD; Thu, 15 Aug 2024 11:00:53 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: Carlos O'Donell Subject: [committed 1/3] Make tst-ungetc use libsupport Date: Thu, 15 Aug 2024 14:00:48 -0400 Message-ID: <20240815180050.1527891-2-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240815180050.1527891-1-siddhesh@sourceware.org> References: <20240815180050.1527891-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1170.7 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 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 Thu Aug 15 18:00:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972867 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=I/KtR3E5; 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 4WlCc72XnQz1yYl for ; Fri, 16 Aug 2024 04:01:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1669D385700F for ; Thu, 15 Aug 2024 18:01:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1669D385700F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723744885; bh=6BZAZxGGhj+ajwnblsa50ewv15JgLuHFJFOOVVLvaqg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=I/KtR3E51cM1AaMFbKIQ7cvj9YPOxTNm+oPqSoOLogosNiBDGH5wiYnwrVhWcCRBw PXUmjrGcE1x+Ude8vvmRrEHSG+YF1wz+7t3ZKEO0Gbx3z+pg2p3+xgUQiaKlrDOZmw ltoBpxkMAAtUQNXfh9JifTgf2dcAKdFI+V1A3IUo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from aye.elm.relay.mailchannels.net (aye.elm.relay.mailchannels.net [23.83.212.6]) by sourceware.org (Postfix) with ESMTPS id 3C3723858D34 for ; Thu, 15 Aug 2024 18:00:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C3723858D34 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 3C3723858D34 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.212.6 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723744857; cv=pass; b=TxwpfmxlDwYudSzVMwWkIlqC3PFrBQRb2CrzXmaBJWbD5ifjB+/Ls8vwQSF0WiRL8YbpHf4yy6kAU/obIhOqx34/h0DWxP+2nlVNLePJ3ha/Bf/VD5Seej5IViqN2nezuMcu/rsu9xNlmcIxqT3ILCffMh6Kfd7eT9DNt3hSNes= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723744857; c=relaxed/simple; bh=R/CfpNUZs4WSbvZHHJTpUaVOeshPG4qWMTNwMb6ZlNY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=L+KYg5gIQRKDtG8pbNRelqmhxfv/kwEc7dvuwzYLACr+HqP19VpYaveYSJnCZpnKbGjSJrrdE7DIlw0FA4Q1rfxRMKMUu5ME+ro/NvwKuMJth80g2VOEH5kTOCZbmWlc0APzX0ULmulHyKXjel3xxxxvZXIbnppB6w2PpUtwFWI= 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 37D2184282; Thu, 15 Aug 2024 18:00:54 +0000 (UTC) Received: from pdx1-sub0-mail-a246.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id DDBF483CFB; Thu, 15 Aug 2024 18:00:53 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723744853; a=rsa-sha256; cv=none; b=rbOSuTH0n8kQH1LBODO153bh8zXgWfCy4sve9T7Vgfww6Z5c4ljCLKEGyiSDHSZeJ33was qCY/2Y0bLRKCCDM39e4M0vzJ5iGfv06sIGGRexqXSfcoHhexo6GoZkjSdFIEWMpKOW05My SSdc7/vlWvrD3ZAzPwJuxiA7BhVpv9fNMMbEQX6z/fPYAOFLp0BbPcjYhOHrfvYvSoFCCu JRKiHcWJflIZzZ2BmhB5MzSmml1x6otaL4hlq53TgUc7sSyIGR8Ioe5BKJXgvzGsGQSLzF LLwvdZkNmnUG/CHQkD7vvMP89ZTNgkFXSJAGnQthhNqxmB88z9PO0dwSnrBKDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723744853; 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=6BZAZxGGhj+ajwnblsa50ewv15JgLuHFJFOOVVLvaqg=; b=vml4V1GwqTQYT96PBo5kza028ns/f4neEq9oxS8g8eeVOPsu+vMkzYhWES7IoB6CSuMaPp UIVzTpO6XK46b6I9JI6gBiI1gje2C3DSR0Ci0ijMxkeFxA7NsFJo8ZMlHa0SSeOyCy36rr j+B9kjPcfjLO/8ULbQ4A0mdKI4F36oS7O+zfVGxV/laaLFAsrTQzxBRTuaGVhn2Vd0+Fw5 wRnzt6yRZVHzpUn5E1RuNHDLocHsL9vP7whdILRMzxTDG0htKnZ2BKRzT5J06hnAZzNxyO jAoy6IVcquetIpT7ifJpf/gIgRUJewVSMMQrYj0D+UU0nWESo0dGhg5YYBzjkQ== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-4sdwt; 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-Decisive-Versed: 3c8343cd6aeecf78_1723744854124_2549138743 X-MC-Loop-Signature: 1723744854124:1750426762 X-MC-Ingress-Time: 1723744854124 Received: from pdx1-sub0-mail-a246.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.127.52.227 (trex/7.0.2); Thu, 15 Aug 2024 18:00:54 +0000 Received: from fedora.redhat.com (bras-base-toroon4859w-grc-84-184-146-171-51.dsl.bell.ca [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-a246.dreamhost.com (Postfix) with ESMTPSA id 4WlCbT40mPzCS; Thu, 15 Aug 2024 11:00:53 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: Carlos O'Donell Subject: [committed 2/3] ungetc: Fix uninitialized read when putting into unused streams [BZ #27821] Date: Thu, 15 Aug 2024 14:00:49 -0400 Message-ID: <20240815180050.1527891-3-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240815180050.1527891-1-siddhesh@sourceware.org> References: <20240815180050.1527891-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1170.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, 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 When ungetc is called on an unused stream, the backup buffer is allocated without the main get area being present. This results in 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 0db4e090af..29888a361f 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 Thu Aug 15 18:00:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972869 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=A23A5mPU; 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 4WlCdN0qTDz1yYl for ; Fri, 16 Aug 2024 04:02:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E1D153858403 for ; Thu, 15 Aug 2024 18:02:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1D153858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723744949; bh=b3Uu0vx2NieXSZLRnlp3uTi5pZYxgcy9XbESDqJ6F+Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=A23A5mPUnkJHTNlvTGT+pX4k+ImkiQDbpCqNU2gZDbBjimHqigzoMB1dvf3y60s25 KPyQ5wUo3zM+3eebgnfVi04Xyw85ZZJpPMy2UhDu6FNbV0HMSKboZeZ+xZj3ZI6vlo F+dT0xr/5EZjTYR1tOpDsutCsp07VCBBOfBIPbT0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from giant.ash.relay.mailchannels.net (giant.ash.relay.mailchannels.net [23.83.222.68]) by sourceware.org (Postfix) with ESMTPS id EF4813858C50 for ; Thu, 15 Aug 2024 18:00:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF4813858C50 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 EF4813858C50 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.222.68 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723744859; cv=pass; b=cPoFLnIY9mpw5A7M/6j9StDPfMIDP6SDYZcsDDqw7BDfaxIClBEfoC01+JUOmUOmIAdyxb5FcT7MTRHj7RA4eD97tPLxv2kIgHDLCLIaL+ninyYvTDpbJNnisdUkmQHA7kbqL/KGFqpMw3i/HYboYReqoZkjkSeXB8DjKM2+WT0= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723744859; c=relaxed/simple; bh=xQo7KvdQVFWcMrFqNaMpw2HCf8u/0zlehXAVyZMChn8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=P/KaKjzRGigrr4ZkZvsxC+G9DhH3FnuY50IOYWWY5iwG8ZdbULJkugP952w480zcjsPB0ZzBPWXQJNJ7Ye0MSkOnc09bdZRUTumv71Dq+IpoX+2QlZaS8qL+238tBeEItJWSOFcIN5m0WMD2VIK9OD/g5s3fWEQSdNmFHhIAAUI= 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 9D21A906865; Thu, 15 Aug 2024 18:00:54 +0000 (UTC) Received: from pdx1-sub0-mail-a246.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 48580906476; Thu, 15 Aug 2024 18:00:54 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723744854; a=rsa-sha256; cv=none; b=IziMqkwxJgis+frYi44oNzh3IW03CxDFLeJETDl3EBZhTZTMJ5j084/xOh6A2nBDgiBJPU C72RHCv/HRrtalM/UETMwwnHly6TSkl25hRXWRdkOJKiX9uN5NPtmj6/c+AsWXTvftaiRm KzzTOpDuyiYTf7nGeakcqV3/ycbvNYea0AMGMu6GcK0yU2V6Hm5IsxW569TK5cJyEfYuP4 ilS5Vk/947D+HeJE1tfoSbK7yojpIMkjLSwZ5y0I05GTRwE7DVexq4dPLp0ltoot/EgXaR eWWtqWPHRgqda1fsEabPsl7QIPAr7DtnlBx7QLaw7JyZs+bsgWIC3jiZatjBcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723744854; 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=b3Uu0vx2NieXSZLRnlp3uTi5pZYxgcy9XbESDqJ6F+Q=; b=aHEzy5Qx7XJoQ0r1mufWXgsg+TV/d++PTvUXq067aSATvsT/5rl3Q55o3WPc2jvHqT5iLg OFNYNuHZ6C0rD29ykkpJXfEyJmh/JtsNDVFAmlsrCVv862dlbTAtnRe0JTzgHE6b8U8YZj EFdOe71x905ABOi/imq7uuQ6SdsbxIJTFcnElkYC9QG21thdBFeMZl7idt/QT64mcjNcC/ gJGRU+2xxRQ5n6d36IpPR9yFSDMq4HImXfn1EO/vWwmQgpXV1roA0c/1/cZCrepANxdZms SQRC3JTUy9iteCWMphKcyCh1JjhDxlL6rAP+ujL0DT90GjrCehXU7BJQnX7dog== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-fhd5v; 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-Stop-Inform: 6ba214811977543d_1723744854521_2024837534 X-MC-Loop-Signature: 1723744854521:3030689508 X-MC-Ingress-Time: 1723744854521 Received: from pdx1-sub0-mail-a246.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.126.110.135 (trex/7.0.2); Thu, 15 Aug 2024 18:00:54 +0000 Received: from fedora.redhat.com (bras-base-toroon4859w-grc-84-184-146-171-51.dsl.bell.ca [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-a246.dreamhost.com (Postfix) with ESMTPSA id 4WlCbT6k18z9f; Thu, 15 Aug 2024 11:00:53 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: Carlos O'Donell Subject: [committed 3/3] ungetc: Fix backup buffer leak on program exit [BZ #27821] Date: Thu, 15 Aug 2024 14:00:50 -0400 Message-ID: <20240815180050.1527891-4-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240815180050.1527891-1-siddhesh@sourceware.org> References: <20240815180050.1527891-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_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..35d8b30710 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 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