From patchwork Tue Jan 14 02:03: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: 104724 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 00BB93857712 for ; Tue, 14 Jan 2025 02:06:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00BB93857712 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=I0gxX+iJ 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 2D4473858C5F for ; Tue, 14 Jan 2025 02:03:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D4473858C5F 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 2D4473858C5F 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=1736820217; cv=none; b=QFeolPLFzwWlAECMzCr+VG8FgHziSrfZbPzL0+Y5VMVJmOBCekQz2NFwgPJuebsFxRL8ZX7htIUwXbNHuUIAodStC70DGYrA1atev2X0tn75CVtmpG/zRx8gb48I8Pn4iRvwTTAzFBVHq2pY01q9de1MI19NzkujZZcrdAr9XBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736820217; c=relaxed/simple; bh=O8kHjS0YXXsf0pzyNnZyeDT2fup4hnIdlyVABeddesg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=bQfEraq3alKAU0N4boJsyU2X3dGRSgf3ylG0viN1kDX8e4fZbyNEqNLxILRgPGxK4YrY9cQR3hBB4IIJ14qTE4GyP4gNtB7D27pLGpV6nz/mBj+Cq5GWO/ATQ12PKtWFtSkuafcsIu9/kP5382l5xRON3uDBhHgHjZ+3JTvqhq8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D4473858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736820216; 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=VNPAuWtBSI6c9pHB3KZkfGnu4/+9ZJtNqWw1ON79ZYo=; b=I0gxX+iJQjIrXAoRYZpL73GXWYjYXByqATrM2bJUuCIL1nChhlkU/o3tNSPwapTD/Vt9sw KSYFaYMHri+kzmj/xZKI7SAxA6GlfpbNAOTxaNLyhTYO44dn9GTuBxPw28axqMoktMy3/T N+Q6KXoUMLO0qWMuYES2cjknxDbJVrg= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-oic9olzFOheEave1T_AR8Q-1; Mon, 13 Jan 2025 21:03:35 -0500 X-MC-Unique: oic9olzFOheEave1T_AR8Q-1 X-Mimecast-MFC-AGG-ID: oic9olzFOheEave1T_AR8Q Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385ed79291eso2662242f8f.0 for ; Mon, 13 Jan 2025 18:03:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736820214; x=1737425014; 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=VNPAuWtBSI6c9pHB3KZkfGnu4/+9ZJtNqWw1ON79ZYo=; b=B8aKipZnfAT+M3mZ2tTb0C+N1KXO90/+0yckPUg6I+nn6eyFJnxprHX9BgwrUpneug 3SqLGN7zubLUDkchxKG/uMCs71e9bCVAu5SdLVZ35LWmc2sYMRwM2RRDd3d2gutcUc45 sZTh8GtSa0qA9fmagMJQ/4P0jwS4+sbbNnJbzxtlZpdqDVFQ0euDd+Ra2ck9gY/adZ60 SX/JWeOgTF/9qnTBGVWCoAov/ZPIxZYV/KcwN6pDrdOaMO40JQPAvmMfiu1fVgHL2j8O 4Wm2pNGNf4efxA7yBS5Gk5ht3UmRd+eFF0CJrKAKSN0oTtCco7S+xnmBXb6aP5ErmJAR Levg== X-Gm-Message-State: AOJu0YzbLcXtIsqechJ5kyi2FScjsYV++yiz54jtZUMD9tNCSuqaNjLD e6xmrXW/SV6OPmPky8S3eG5clsggtru4nr96e2UZ0EYx0CD5RLLG/r32+3EVfjolJHb0H4siDYq CO4HS05xBvzb8C6++V/Uy02537L5n2TG03EQCEjG/xzJUzBf0Y3G9DkPbhDJNdbQW4f0kIsU99M WH4pjwc53uEeWA5MtkoOoRmbJiHyCQ9nQniblmY2WJUA== X-Gm-Gg: ASbGncs+cq08XFO7cMQ8Ei5ubEA6EtcFeGqIL32IIkgwX6LciZ0ROEezQtz+P7FB8dp z2YLdjWaSCo21mTCUB3/DCd/3XBkN1SgQJE1U77RLUv3DIVQWJPgW4ysT9pJ2oqacQjiKygDvB6 P39ONTqQtLPn+1auaudWruO0amJyso5Ji8ySDhlafshaYMOGfYBHqg7l0263Hg54XTtT4lgs+wM 5laa8mHR46vv7hXAIXgtg+j37O9ohU2ruLeEVdwgk6BGoF1dpnTU5wi0AVzYoLw4WXnvDTnvZ2c 4/MQMjKna7iU X-Received: by 2002:a5d:6d0e:0:b0:38a:a117:3dbe with SMTP id ffacd0b85a97d-38aa1174168mr6190998f8f.21.1736820213857; Mon, 13 Jan 2025 18:03:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IEKRYTI/v1SREouCd6coaomu46tbcjcZ1FvuOkYvnAq9O1MT/c/Gg3De2V5UvSCKI06I7ySFg== X-Received: by 2002:a5d:6d0e:0:b0:38a:a117:3dbe with SMTP id ffacd0b85a97d-38aa1174168mr6190978f8f.21.1736820213434; Mon, 13 Jan 2025 18:03: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-38a8e38efeesm13770196f8f.62.2025.01.13.18.03.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jan 2025 18:03:32 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.97) (envelope-from ) id 1tXWHD-0000000CvlA-0D7M for libc-alpha@sourceware.org; Tue, 14 Jan 2025 02:03:31 +0000 Date: Tue, 14 Jan 2025 02:03:31 +0000 (UTC) From: Joseph Myers To: libc-alpha@sourceware.org Subject: [PATCH 3/6] Make fflush (NULL) flush input files (bug 32369) In-Reply-To: Message-ID: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: N94328bI_TBxMspzSwuIIW-iNaFuAs9yObTKiNbaRd0_1736820214 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 32369 and required by POSIX, the POSIX feature fflush (NULL) should flush input files, not just output files. The POSIX requirement is that "fflush() shall perform this flushing action on all streams for which the behavior is defined above", and the definition for input files is for "a stream open for reading with an underlying file description, if the file is not already at EOF, and the file is one capable of seeking". Implement this requirement in glibc. (The underlying flushing implementation is what deals with avoiding errors for seeking on an unseekable file.) Tested for x86_64. Reviewed-by: DJ Delorie Reviewed-by: DJ Delorie --- libio/genops.c | 7 ++++ stdio-common/Makefile | 1 + stdio-common/tst-fflush-all-input.c | 53 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 stdio-common/tst-fflush-all-input.c diff --git a/libio/genops.c b/libio/genops.c index 2197bfe7a1..e4378ca48f 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -730,6 +730,13 @@ _IO_flush_all (void) ) && _IO_OVERFLOW (fp, EOF) == EOF) result = EOF; + if (_IO_fileno (fp) >= 0 + && ((fp->_mode <= 0 && fp->_IO_read_ptr < fp->_IO_read_end) + || (_IO_vtable_offset (fp) == 0 + && fp->_mode > 0 && (fp->_wide_data->_IO_read_ptr + < fp->_wide_data->_IO_read_end))) + && _IO_SYNC (fp) != 0) + result = EOF; _IO_funlockfile (fp); run_fp = NULL; diff --git a/stdio-common/Makefile b/stdio-common/Makefile index b5f78c365b..48fbf05a85 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -238,6 +238,7 @@ tests := \ tst-fdopen \ tst-fdopen2 \ tst-ferror \ + tst-fflush-all-input \ tst-fgets \ tst-fgets2 \ tst-fileno \ diff --git a/stdio-common/tst-fflush-all-input.c b/stdio-common/tst-fflush-all-input.c new file mode 100644 index 0000000000..e9df3a0c08 --- /dev/null +++ b/stdio-common/tst-fflush-all-input.c @@ -0,0 +1,53 @@ +/* Test fflush (NULL) flushes input files (bug 32369). + 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) +{ + FILE *temp = tmpfile (); + TEST_VERIFY_EXIT (temp != NULL); + fprintf (temp, "abc"); + TEST_COMPARE (fflush (temp), 0); + TEST_COMPARE (lseek (fileno (temp), 0, SEEK_SET), 0); + TEST_COMPARE (fgetc (temp), 'a'); + TEST_COMPARE (fflush (NULL), 0); + TEST_COMPARE (lseek (fileno (temp), 0, SEEK_CUR), 1); + xfclose (temp); + + /* Likewise, but in wide mode. */ + temp = tmpfile (); + TEST_VERIFY_EXIT (temp != NULL); + fwprintf (temp, L"abc"); + TEST_COMPARE (fflush (temp), 0); + TEST_COMPARE (lseek (fileno (temp), 0, SEEK_SET), 0); + TEST_COMPARE (fgetwc (temp), L'a'); + TEST_COMPARE (fflush (NULL), 0); + TEST_COMPARE (lseek (fileno (temp), 0, SEEK_CUR), 1); + xfclose (temp); + + return 0; +} + +#include