From patchwork Tue Jan 14 02:02:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 104723 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 BE5933858288 for ; Tue, 14 Jan 2025 02:05:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BE5933858288 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Faujb+tf 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.133.124]) by sourceware.org (Postfix) with ESMTP id 65E95385800F for ; Tue, 14 Jan 2025 02:02:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65E95385800F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 65E95385800F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736820157; cv=none; b=VfxNkYdj9jEB1aMxlTlWJ/T/n6XJbpFDtc0pKhw6O2ALSrgRL6v7DUmXBVGTIw5pvG2SuoX5zuwXZNCq+LY2314uItIHotiGSkHkGWrrokHoQRx1A4u3nSWesmmgQ7Ti9YFr3GEFZQouwhN/B5X9zkspHk+YXduD8Swe28YrfiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736820157; c=relaxed/simple; bh=6gjJcnJrjj0cLtjSfaZWnyn2fCr5Zd0Hx258er3GrcE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=tPNlyIadC93aKVIUC9JO2sFf+6ZWBWKXVn+NXnVunn9lyd4ArnbqCUWVS7YaEGfTaq69ikAWALZpG/Cqnerom+tr1LVwWrCPSNvl5EqeuT6/H4/EzKikGegVqg20osyTA+bZAsXTXJ5S1K+c5hp+l1ywMa8gcuwWzXQxBahpvhA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 65E95385800F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736820157; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=P3eJpJnUb1+KMVQ0N+M3XtttQBM3pj7zVR7DkeILtms=; b=Faujb+tfESB1XJocZ6xqq7GCN8jLMge902i3nvxlsXeEwIFaFz8bGZK/Sk40IIUX7kDim8 boYcQBKFSZ3+ItLbwCvEAYdqnOweHnzWGAYu0Mag760zS7BFuiRvvNOZQhxnskwcoSZAln GVywi3F7V5MyllvsjYKPCq6FjaEqkzY= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-228-LuDUjPskNKK1xxTWCVXOzw-1; Mon, 13 Jan 2025 21:02:35 -0500 X-MC-Unique: LuDUjPskNKK1xxTWCVXOzw-1 X-Mimecast-MFC-AGG-ID: LuDUjPskNKK1xxTWCVXOzw Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-385dcae001fso2204167f8f.1 for ; Mon, 13 Jan 2025 18:02:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820154; x=1737424954; h=mime-version:references:message-id:in-reply-to:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=P3eJpJnUb1+KMVQ0N+M3XtttQBM3pj7zVR7DkeILtms=; b=nQDAeSo4Cdt6X1f9EPptxyAxpZAqZb5Fz4ux8rJuPwUHWibFgGi4SJx4rB/8SFTxOL Yb7nyUO8N1w3Dn+XiWsP62LPSPdHiqMy+UEsRMtwsFR8j1JsGItujge8g/E+S9fyhGrD ugQwgMchFjJeXB7R7dQGTIfB9ah80Ku6dWG3hLaYFBfTIvKnN3OvhcYs/6wWOIKiLuyZ SXIhFLHAjK/TPOe6J5zvbWiifSRaPvgtzwM8iq4X5vGPbrKJbr7Fa68qP2FzKppmjaBy dbVKRwDO7yvXU02fiId2tRYb55NpKoaHB+D7WSQJsDxIQVzYWyC1k0Jv7LD4vDp1j/9/ aOmQ== X-Gm-Message-State: AOJu0YzAxpoINrp2vTneZU6WZnR7PP8ZdsrmuU03nEo36au2p0nNyUdN +ILzkPaszvvdyCntxoRNHskrqIvs3Sfnjg4tc+7EMsiWEbAoHh5RKP6zdbPKyRGRU2jusLOga+x 2vGwVbsJ8D8B604WCRW8Uy43UrZQXXZHzGQvZ1WuNrfYj2muXQ1w8REMjo0Y06qdH3E7F0bvLfT yVJACPdmbm1fzGMhJ6g6JRuD3xoyHM4r9HC+mER4EiEw== X-Gm-Gg: ASbGncsoAG3sL05yaalQ5/mAMqPSLfClruE8Q9J2HxTiRQnDx3rfhBCgdWwqvUxOALv hLVCXGgIr7/Dp5jhInGn566fypzzJvTN3dCrG/G4xQyvBMkE4JP8RBNdmjjWJbTCcjeYq8IqOjj h5vkk3M91p/1ZFG4v17nNPeLIUN8gFF6v1oRPvEdIXXpLzkzG1d2dWeKGdLRupMPjqio8tNWeKf JDilK4fnLHxEhPQuFThOOj5tF+82hCjOzmVt1CUDmdm2LjQZM2HlkP6Pv41iig/NtqzwVlMvnN/ spbTjF/UO+nh X-Received: by 2002:a05:6000:401e:b0:38a:4184:151c with SMTP id ffacd0b85a97d-38a8730ad01mr20093708f8f.27.1736820154227; Mon, 13 Jan 2025 18:02:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IEuoJZloFAkGyBlb0nlOwQGxs23dRueq6FUSpLUt49CMQ0MkQVK9TuFfO5saFEf9Thh187Ncw== X-Received: by 2002:a05:6000:401e:b0:38a:4184:151c with SMTP id ffacd0b85a97d-38a8730ad01mr20093685f8f.27.1736820153727; Mon, 13 Jan 2025 18:02:33 -0800 (PST) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a8e38d013sm13774269f8f.58.2025.01.13.18.02.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 18:02:33 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1tXWGF-0000000Cvjo-0Nag for libc-alpha@sourceware.org; Tue, 14 Jan 2025 02:02:31 +0000 Date: Tue, 14 Jan 2025 02:02:31 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: [PATCH 1/6] Fix fflush after ungetc on input file (bug 5994) In-Reply-To: Message-ID: <3cbb9e5a-f404-7294-bdef-3ff606d8c8f4@redhat.com> References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: woN0STC3v-822fnL4Av4we8wbNRAD2MdNXHsz6viNdY_1736820154 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org As discussed in bug 5994 (plus duplicates), POSIX requires fflush after ungetc to discard pushed-back characters but preserve the file position indicator. For this purpose, each ungetc decrements the file position indicator by 1; it is unspecified after ungetc at the start of the file, and after ungetwc, so no special handling is needed for either of those cases. This is fixed with appropriate logic in _IO_new_file_sync. I haven't made any attempt to test or change things in this area for the "old" functions; the case of files using mmap is addressed in a subsequent patch (and there seem to be no problems in this area with files opened with fmemopen). Tested for x86_64. Reviewed-by: DJ Delorie --- libio/fileops.c | 5 +++ stdio-common/Makefile | 1 + stdio-common/tst-ungetc-fflush.c | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 stdio-common/tst-ungetc-fflush.c diff --git a/libio/fileops.c b/libio/fileops.c index 775999deb3..27e1977eb6 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -799,6 +799,11 @@ _IO_new_file_sync (FILE *fp) if (fp->_IO_write_ptr > fp->_IO_write_base) if (_IO_do_flush(fp)) return EOF; delta = fp->_IO_read_ptr - fp->_IO_read_end; + if (_IO_in_backup (fp)) + { + _IO_switch_to_main_get_area (fp); + delta += fp->_IO_read_ptr - fp->_IO_read_end; + } if (delta != 0) { off64_t new_pos = _IO_SYSSEEK (fp, delta, 1); diff --git a/stdio-common/Makefile b/stdio-common/Makefile index bbdc1ff709..589b786f45 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -302,6 +302,7 @@ tests := \ tst-swscanf \ tst-tmpnam \ tst-ungetc \ + tst-ungetc-fflush \ tst-ungetc-leak \ tst-ungetc-nomem \ tst-unlockedio \ diff --git a/stdio-common/tst-ungetc-fflush.c b/stdio-common/tst-ungetc-fflush.c new file mode 100644 index 0000000000..a86d1fdb7f --- /dev/null +++ b/stdio-common/tst-ungetc-fflush.c @@ -0,0 +1,64 @@ +/* Test flushing input file after ungetc (bug 5994). + Copyright (C) 2025 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 +#include +#include + +int +do_test (void) +{ + char *filename = NULL; + int fd = create_temp_file ("tst-ungetc-fflush", &filename); + TEST_VERIFY_EXIT (fd != -1); + xclose (fd); + + /* Test as in bug 5994. */ + FILE *fp = xfopen (filename, "w"); + TEST_VERIFY_EXIT (fputs ("#include", fp) >= 0); + xfclose (fp); + fp = xfopen (filename, "r"); + TEST_COMPARE (fgetc (fp), '#'); + TEST_COMPARE (fgetc (fp), 'i'); + TEST_COMPARE (ungetc ('@', fp), '@'); + TEST_COMPARE (fflush (fp), 0); + TEST_COMPARE (lseek (fileno (fp), 0, SEEK_CUR), 1); + TEST_COMPARE (fgetc (fp), 'i'); + TEST_COMPARE (fgetc (fp), 'n'); + xfclose (fp); + + /* Test as in bug 12799 (duplicate of 5994). */ + fp = xfopen (filename, "w+"); + TEST_VERIFY_EXIT (fputs ("hello world", fp) >= 0); + rewind (fp); + TEST_VERIFY (fileno (fp) >= 0); + char buffer[10]; + TEST_COMPARE (fread (buffer, 1, 5, fp), 5); + TEST_COMPARE (fgetc (fp), ' '); + TEST_COMPARE (ungetc ('@', fp), '@'); + TEST_COMPARE (fflush (fp), 0); + TEST_COMPARE (fgetc (fp), ' '); + xfclose (fp); + + return 0; +} + +#include