From patchwork Tue Jan 14 02:04:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 104725 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 548423858C5F for ; Tue, 14 Jan 2025 02:07:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 548423858C5F 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=hG1q1U0T 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 ESMTP id C69CF3858D38 for ; Tue, 14 Jan 2025 02:04:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C69CF3858D38 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 C69CF3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736820269; cv=none; b=Y4+g63EX/TRgJi9Q8JzJ/8wRdzdxtBUNiYXsS0qnNXvsPo75C0IVZPiop8O+c+Eqd9+4O93RwCshKsaVrUuC5iO/rFhiqpQnLV7o8D6xmJhFZ5JwHJADekZN9QR1qnuLjTzUCGRyG8sRCsrZ0QYK5a+xSGnpbaUh3IHEUSQnNsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736820269; c=relaxed/simple; bh=JfEQufexQmKgq2+K2lKNss0Ueow7RnKAtuvRFyXKtZw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=G/cRQxae2AWEGF903Fzjy8OfIZNOzjagQrSQnEo9WVuof6YRedCGPoO3zrhEpuKUrmzeO265neWdpTETM+7QXpNCHGmC9iSfWn8pqi2AWVoLAsUHQFYwQoeBqGQOa4URIcNoGIX6uh8xEkORk7RTl+IPAT391IridCJw58+au6c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C69CF3858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736820269; 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=x+27KXVCCn0eBoLZfJdzzDHRvoocbWZRuPnQw8xV02A=; b=hG1q1U0Tk2fUvrbAyaMoaGRSfmBvfsa03bW+oxc6hE60mfeVud7qXkKiaP4onBtYcQ2ETv VGw0i8j/+ap3yWSpKfF2za3M3p5ZXyZYmkJmWLgOHQ7mRfQua4OZFQuBR9O6I1vjzn9WL+ 5UI90Xx4gAZ/kk6VzwOeu3mcJnWntmM= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-615-JHIwgTF0N9WS59rLGZKZ2w-1; Mon, 13 Jan 2025 21:04:28 -0500 X-MC-Unique: JHIwgTF0N9WS59rLGZKZ2w-1 X-Mimecast-MFC-AGG-ID: JHIwgTF0N9WS59rLGZKZ2w Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43625ceae52so27214715e9.0 for ; Mon, 13 Jan 2025 18:04:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820267; x=1737425067; 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=x+27KXVCCn0eBoLZfJdzzDHRvoocbWZRuPnQw8xV02A=; b=OZq4nLz50UmO+zmxKVEQzkJLq8v80U0LB1DBOoAWhdkGmX+ScJFII/i8T3t+8RzMuI sPu1hNS/hEp3SMf918e+gZ/QOwRxGbR+dTRXv9gr7INbVIVUg8KjfgpmIeDmEEt22S15 Ln6/80thvWfO9BbWwkw0jA/ramkbt3uD/+b9ZKg3sIFa+Z8nQPJg1NKMBS7sqe6SZeXQ /RauBOstpYQioc5/2qlRhIccbGP7N8QvGh24mm0lmziju0gwLc0WrLyL9i9w9qrRPJNw y0cbqoHdJXKdwKGoD/UTHoRBc1pgRrcwTDtYuAlV66AtIxRR02GHngOksXMe4hB3gY8N 6zAA== X-Gm-Message-State: AOJu0YxbxOuUkfmp0E2bwfcLULmKtT284s3IJA+Cyh4sXYAneASDCQbL eFoGShbWZaflA0GI6zU034jYDuhprzShcZdGmHU4SSzNlPk+FK8d5jKx+Ncc+ZaiR8fH8MbPRDI qaPhnNkZddTj2xJmTB5Wc2y0koGoylyC6Ag/up19zAPtWi/RSuXVh95bzB3LDYMzfKj0H6l22ZE Sglk77uhVUagsI54Hvb+WZro+HCAd5kKoAdqGKZn2Dqg== X-Gm-Gg: ASbGncsPXAykMuhededios4y/y76Qo8Q+WbnqpShJxPPcV7slyBhA1s2R52s0b1Oike 7d0XB1PZNe3VBtQOxLdDTEdIfKfGlMFZJN2IklbVHCpVIY55fBrjXD7I79ygtTRzMFdtLw95Fpu N/Wv8nwvTO2kX24qCkFmEoE6+h+HmL/aUmmK0optZcg333avyqz0UE8Uu+oFKk+Jk97dGMjk6tc AaMHtw6WyDr9/mNOjNWAcOqbr5Ay5j5vf1xLog4E9mUA4WcUJJ23JzG7q1EqGiCWDOl6tnnr5MN F9RhuuPOOmXk X-Received: by 2002:a05:600c:4449:b0:435:192:63ca with SMTP id 5b1f17b1804b1-436e26f481dmr90696425e9.21.1736820266718; Mon, 13 Jan 2025 18:04:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IH+L44grC+hwYAG9pec+OScjXWIuNGJygYIOFdxEw8ePfx4b+6h4WPgy5aVYtjXyYF6UCSQEQ== X-Received: by 2002:a05:600c:4449:b0:435:192:63ca with SMTP id 5b1f17b1804b1-436e26f481dmr90696265e9.21.1736820266229; Mon, 13 Jan 2025 18:04:26 -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 5b1f17b1804b1-436e2e89e14sm194771395e9.33.2025.01.13.18.04.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 18:04:25 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1tXWI3-0000000Cvln-2m9k for libc-alpha@sourceware.org; Tue, 14 Jan 2025 02:04:23 +0000 Date: Tue, 14 Jan 2025 02:04:23 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: [PATCH 5/6] Fix fflush handling for mmap files after ungetc (bug 32535) In-Reply-To: Message-ID: <8ef14304-75b4-6938-1cd4-d44c3390f7e0@redhat.com> References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: A0qRpVEPN7IydRZhbAA-um6hDGl6KpTKDUjVuXjkxRk_1736820267 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.2 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, 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 32535, fflush fails on files opened for reading using mmap after ungetc. Fix the logic to handle this case and still compute the file offset correctly. Tested for x86_64. Reviewed-by: DJ Delorie --- libio/fileops.c | 12 ++++++--- stdio-common/Makefile | 1 + stdio-common/tst-fflush-mmap.c | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 stdio-common/tst-fflush-mmap.c diff --git a/libio/fileops.c b/libio/fileops.c index 97875d1eaf..7e370d12b2 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -858,17 +858,21 @@ libc_hidden_ver (_IO_new_file_sync, _IO_file_sync) int _IO_file_sync_mmap (FILE *fp) { + off64_t o = fp->_offset - (fp->_IO_read_end - fp->_IO_read_ptr); if (fp->_IO_read_ptr != fp->_IO_read_end) { - if (__lseek64 (fp->_fileno, fp->_IO_read_ptr - fp->_IO_buf_base, - SEEK_SET) - != fp->_IO_read_ptr - fp->_IO_buf_base) + if (_IO_in_backup (fp)) + { + _IO_switch_to_main_get_area (fp); + o -= fp->_IO_read_end - fp->_IO_read_base; + } + if (__lseek64 (fp->_fileno, o, SEEK_SET) != o) { fp->_flags |= _IO_ERR_SEEN; return EOF; } } - fp->_offset = fp->_IO_read_ptr - fp->_IO_buf_base; + fp->_offset = o; fp->_IO_read_end = fp->_IO_read_ptr = fp->_IO_read_base; return 0; } diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 06c9eaf426..703e8af7e8 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -239,6 +239,7 @@ tests := \ tst-fdopen2 \ tst-ferror \ tst-fflush-all-input \ + tst-fflush-mmap \ tst-fgets \ tst-fgets2 \ tst-fileno \ diff --git a/stdio-common/tst-fflush-mmap.c b/stdio-common/tst-fflush-mmap.c new file mode 100644 index 0000000000..fd3671f198 --- /dev/null +++ b/stdio-common/tst-fflush-mmap.c @@ -0,0 +1,48 @@ +/* Test fflush after ungetc on files using mmap (bug 32535). + 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-fflush-mmap", &filename); + TEST_VERIFY_EXIT (fd != -1); + xclose (fd); + + /* Test fflush after ungetc (bug 32535). */ + FILE *fp = xfopen (filename, "w"); + TEST_VERIFY (0 <= fputs ("test", fp)); + xfclose (fp); + + fp = xfopen (filename, "rm"); + TEST_COMPARE (fgetc (fp), 't'); + TEST_COMPARE (ungetc ('u', fp), 'u'); + TEST_COMPARE (fflush (fp), 0); + xfclose (fp); + + return 0; +} + +#include