From patchwork Tue Jan 14 02:03:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 104727 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 CC1023858C52 for ; Tue, 14 Jan 2025 02:12:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC1023858C52 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=HkQwzYIM 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 D984C3858C52 for ; Tue, 14 Jan 2025 02:04:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D984C3858C52 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 D984C3858C52 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=1736820245; cv=none; b=wCb4rn0Ye8Hz1dGPYCkt8ZVv/wGHwPj+Wgo4PJttLdpkMzYoZpYZkdBsC9R/OJ7CewFH13bWUNlDdwJ8w9J09WXjeLqx/wCOFYA+xmU9koPtpDlF7t3FKFQVQduSolzobJuzi57Y3TiSx1ecvARhtG+7WKSoEo2oZcnHr67dUVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736820245; c=relaxed/simple; bh=9w2JF115U/ZtD1I25P7I5RygHKyWAbxlV8T43J5iCMk=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Vh4EKdzpcFf0c3jVAex3gZnKo/mw5SsfCBt9xqLM/M87H6Yt0wqGX4Z3nhfgJBaFwRxMS2S0D8MFToMjRc2VxPsxxazCVA2jDaYO5vChtH5YM9jxkzM9giTdasS/yqwtvooDgiZ+MAPyBUR20AOQw6ERiVPjQz/8gjC95cITueo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D984C3858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736820245; 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=bAMusX4vnU61BAFHq/j9+tXQ1KHkLC4QfXCtxACwG20=; b=HkQwzYIMDyxOpkMP6zRXGpgWBh+W48aFH1FlAloHnv7mbEwT3shHL7MPbSMW7cOd7+kYG3 2owJ1YofRn3Krrt6R9RvM8cbIwnWv9VwLhsOzMeqm4BnnSYQC4YWAiQBk7xWMg4rkf71LI 3CcILO2+lNfshGLmeqW5wKSD7JQkqpE= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-645-FJI9VvsfMVe3nHIrDsSaJQ-1; Mon, 13 Jan 2025 21:04:04 -0500 X-MC-Unique: FJI9VvsfMVe3nHIrDsSaJQ-1 X-Mimecast-MFC-AGG-ID: FJI9VvsfMVe3nHIrDsSaJQ Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-43631d8d9c7so23859505e9.1 for ; Mon, 13 Jan 2025 18:04:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820243; x=1737425043; 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=bAMusX4vnU61BAFHq/j9+tXQ1KHkLC4QfXCtxACwG20=; b=fNGTG5rzPfwVbp3vXh80i6//Ow370jwsXakFFiYjU94FSn+SFYsRlgIaoIT+yYJz0w dUnJWxpnWFFGGLHIZ4U6P7Xz9wsKDujlxfHI1K3/2kXDAPFLrbcAKxl3MthHNrwa0dVm 7iCWs52K1+ceG7AuqghWEAiSCB7cBaAvBXLBMwGHXA6NmmTA66YgBdWT/FFbYK7eYwoT zVIxhJPBu//ogXoDWIJ9JHKo3exV6ZLUTp/+CIY3CTBrNwe+tfR0T4wOUh3oodoh7Qfg AMS8DQNHEDAHyeDms/VL/qVddhaOB6l0YzfeoUYLuNbmHDsYzedZNiUNPdxLro9OHQQb X34w== X-Gm-Message-State: AOJu0Yz7VdO5XjPbq2rDiEbCK3UVbxT8CaVf8kDj6KAGvQn5rCJpep1P LaPNAGw786WU1Fbx+b58dUBAJ98mRotoIds5p2gQY8nLQTwO0K7DZKCSlI+Q0u8hN8WNPkdsT2K poNC+Fd7rpvJ9aX6S9/+zJqiE0050TKVZZdEZvoTQE0we4P7B9gW6AMMH04r2zolOtvYH6QWjeF eE8Sxa6VrqIWNnoqygGUFIYk3+FgWzVONukbn1m1PwpA== X-Gm-Gg: ASbGncsXi7mGVR48gOec5lMXWn1SPKcaNgxBxZhJ8kfcTunmVvOLkhiS90xJSTT8WnW /YesiAKaHr+HkrA0M2NJ8WZREBxFCbugcs5V0F34WtTSD4vNwih1ZKewwvfJ5FaTpmUk6I+sgrb 3ospx6+eBK2Ak+I5td2Wa5BEWl+rZ5xeBYms4+gz6njPCO1N+IBCw6AFZS0HGrQHhdqDhmb3xEC 1+njqIIl9SV3NV1LFfjU0ncoxhIq/bcIuVJF4B0r21emF87M8R1FbJj8uMPYEIZvJfbzmMnT20W w+kKNIf5/Bk7 X-Received: by 2002:a05:600c:1e1f:b0:436:fdac:26eb with SMTP id 5b1f17b1804b1-436fdac27d7mr37874515e9.7.1736820242740; Mon, 13 Jan 2025 18:04:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IERkyZgEb0Z5lMGNjodjPrlFrF0XyseX862iCNJA7R4iu15pLlSyJ3xkjmOiroOENAHP/JrrQ== X-Received: by 2002:a05:600c:1e1f:b0:436:fdac:26eb with SMTP id 5b1f17b1804b1-436fdac27d7mr37874375e9.7.1736820242142; Mon, 13 Jan 2025 18:04:02 -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-38a8e4b8209sm13689407f8f.70.2025.01.13.18.04.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 18:04:00 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1tXWHe-0000000CvlZ-3XB3 for libc-alpha@sourceware.org; Tue, 14 Jan 2025 02:03:58 +0000 Date: Tue, 14 Jan 2025 02:03:58 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: [PATCH 4/6] Fix fseek handling for mmap files after ungetc or fflush (bug 32529) In-Reply-To: Message-ID: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: udXwEh0nENDfKkw26HIPmyYPYNjAiO8Q0z1L6zUfjBc_1736820243 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 32529, fseek fails on files opened for reading using mmap after ungetc. The implementation of fseek for such files has an offset computation that's also incorrect after fflush. A combined fix addresses both problems (with tests for both included as well) and it seems reasonable to consider them a single bug. Tested for x86_64. Reviewed-by: DJ Delorie --- libio/fileops.c | 9 +++++- stdio-common/Makefile | 1 + stdio-common/tst-fseek-mmap.c | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 stdio-common/tst-fseek-mmap.c diff --git a/libio/fileops.c b/libio/fileops.c index 358378431f..97875d1eaf 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -1106,11 +1106,18 @@ _IO_file_seekoff_mmap (FILE *fp, off64_t offset, int dir, int mode) if (mode == 0) return fp->_offset - (fp->_IO_read_end - fp->_IO_read_ptr); + if (_IO_in_backup (fp)) + { + if (dir == _IO_seek_cur) + offset += fp->_IO_read_ptr - fp->_IO_read_end; + _IO_switch_to_main_get_area (fp); + } + switch (dir) { case _IO_seek_cur: /* Adjust for read-ahead (bytes is buffer). */ - offset += fp->_IO_read_ptr - fp->_IO_read_base; + offset += fp->_offset - (fp->_IO_read_end - fp->_IO_read_ptr); break; case _IO_seek_set: break; diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 48fbf05a85..06c9eaf426 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -261,6 +261,7 @@ tests := \ tst-freopen64-7 \ tst-freopen7 \ tst-fseek \ + tst-fseek-mmap \ tst-fwrite \ tst-fwrite-memstrm \ tst-fwrite-overflow \ diff --git a/stdio-common/tst-fseek-mmap.c b/stdio-common/tst-fseek-mmap.c new file mode 100644 index 0000000000..86fa99a1a2 --- /dev/null +++ b/stdio-common/tst-fseek-mmap.c @@ -0,0 +1,59 @@ +/* Test fseek on files using mmap (bug 32529). + 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-fseek-mmap", &filename); + TEST_VERIFY_EXIT (fd != -1); + xclose (fd); + + /* Test fseek after ungetc (bug 32529). */ + 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 (fseek (fp, 0, SEEK_CUR), 0); + xfclose (fp); + + /* Test fseek positioning after fflush (another issue covered by the + same fix). */ + fp = xfopen (filename, "rm"); + TEST_COMPARE (fgetc (fp), 't'); + TEST_COMPARE (fflush (fp), 0); + TEST_COMPARE (ftell (fp), 1); + TEST_COMPARE (fseek (fp, 0, SEEK_CUR), 0); + TEST_COMPARE (ftell (fp), 1); + TEST_COMPARE (fgetc (fp), 'e'); + xfclose (fp); + + return 0; +} + +#include