From patchwork Sat Mar 8 05:41:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Zeter X-Patchwork-Id: 107537 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 A83BC3858401 for ; Sat, 8 Mar 2025 05:43:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A83BC3858401 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=JNsdeQcq X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 951693858D1E for ; Sat, 8 Mar 2025 05:42:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 951693858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 951693858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741412539; cv=none; b=O6T1XNgoWXyVFegpYpTWis2txTIajNqBk/P4wRrLOrywzjcjZA9+5d3hlKuWjBH62ZB9nmONIJaC5rUXDt+92w8afA3BAuBPXCxeAfGBG/gLOJF2Tp6eEIe3zFkkX5PrHfUrN2oWEJB0QJGeZQtQOKRPd0Dhe0Dv3dkHp1ZDM4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741412539; c=relaxed/simple; bh=uV1M81RtwalP0/LmNZmtD2mW3/Gpc6JFhEzGkUxc4p0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=meRa0/Y5GSm4TA/Gp2sSBpaqbsS8ekCTv0jsRoUr3+7qNQGNLqUX0d5pYu8tP9iI/Chy4oBR5sV9yiLeG0vWKPWLL9LJbaFYe2QNckTyLnicim6/UfjC8NCza6iF8ZplryUQopuqpKhFwUKO4XF2v31j0VA/HjiiWLgq7Drpsco= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 951693858D1E Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-22355618fd9so49099835ad.3 for ; Fri, 07 Mar 2025 21:42:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741412538; x=1742017338; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nIDfzKMPkPta9l9UuUFKTrmAsdMvic9y5eC+/pUl6Pc=; b=JNsdeQcqpINpwe/WV4w8zUqIx2ckWkAILNlogaj4Bm5jLPFrFVtavs5epUIakbTtsT yP2Wh7N4+vRYnrHSmvnPGAZVSQZtfHVhW/7rFx5Mv7gUVQjp+i9tZLlZ1cIxPi0P6nrI 7xipt3VA2NRAofh6+M0i4FA8XLppuZnbGyk3jtkduWRznO62Eye3uHLfOEFSsgnjROle k8TAkevqOe3EbAnC8x3ZbptOiQHZk+6xwlfs3LgCNGD/xly9ug53E8dOivRMMgWFjMKn Z8wxUO1cagOvIhhEWULu39/1Wl2wc4Z4nN0ZWMDCChV6nB2AhYNN+SWkpkrzjQkuam/b H19w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741412538; x=1742017338; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nIDfzKMPkPta9l9UuUFKTrmAsdMvic9y5eC+/pUl6Pc=; b=QwUcb2BMqaD0j1yYoeiDcxoiE1wrvXOFhSXB/ZzME78+zFn4BZ5/sI/IuKDhpOaYaA h9YWDBTNiGurUhQE4AENhi7+MxzqFghDeYTIO4wL3pCRc0B56D2ZRaNz4kLAj0Wv95jl 2YwU4QdPpx1bFMdSf1jiPJZpJDtHBng7vdRKk8uVO1pIwyjp+T0eI9ruKRt2S1bQjKBr GWnum7WTE/TO18FhapX9L0AbmG6q2WfCu0RHNX2Cig78bic3Y1nCriVXRUzncDQiEsHc EwbI4wSrgz18VD8PRlKnnj8trVY/XRLC4uJ/UL4MazIQ42ttWbZbLFox+nrKB5oNOsyi VpMA== X-Gm-Message-State: AOJu0Yys79YqKSma1Mn77AUGl/YFnlI6JfRUGVaGj6pf0n2Etlybyg1a QmaCxCZgozN019OUy8taAUWVbxP8yuQUMD+YuXatcjujfgjIwMJC9tldMPkH X-Gm-Gg: ASbGncvVFHJCom+BcnsUzz15qpjs9pl3VWuczx+pQpF7of6wGK8h7oACAxA3UwLqft+ IBM3AHC6ZVjtxoqAVDzuqBiVpITjKDUlzXz9EvjXNDLuOvicTvdTlzkKrYEc+iyBeoyKqttrE5I btmqQK2d1WdU2el5AwVEQH3KhrCFT7HrfJ2aC9En/iPFtZM2WDpddnpYCJBn+kBJOpxbzCwhAw0 xan4DVYdQvpvFQx0+su+pgipbG8iCQf7+09Jp6s85CVQgY/qr9A/do3F40tztHONLA+FlPw0Xk8 arzzDzANYxxEgt5b3Of6eS1NaSxV5mhvpFs79Ed5+6yjHaDrzSjmJNBjGYnm/tbK0gDfaccG X-Google-Smtp-Source: AGHT+IFQNZYAI9U+17+ex6upeKhDO6BQPUx1rNyTScLkiMo5oJ5S7IiNr4bdbAY/CkW3ZKbKwC1D7g== X-Received: by 2002:a17:903:2f86:b0:223:f408:c3d8 with SMTP id d9443c01a7336-224288942e6mr91249705ad.16.1741412537622; Fri, 07 Mar 2025 21:42:17 -0800 (PST) Received: from fedora.. (203-219-124-199.tpgi.com.au. [203.219.124.199]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410a80f4asm39656215ad.134.2025.03.07.21.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Mar 2025 21:42:17 -0800 (PST) From: Samuel Zeter To: libc-alpha@sourceware.org Cc: Samuel Zeter Subject: [PATCH] libio: Fix orientation [BZ #22796] Date: Sat, 8 Mar 2025 15:41:51 +1000 Message-ID: <20250308054156.13888-1-samuelzeter@gmail.com> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 When using fread, stream orientation is only set in the case when less than a buffer's worth (4096 bytes) of data is requested. In this case the __underflow function is called, and the orientation set. In the case where more than 4096 bytes are needed, the __underflow function is skipped, and hence the stream's orientation is left unset. Setting the orientation to byte oriented before any buffer filling logic ensures that the orientation is set correctly regardless of the amount of data requested. Signed-off-by: Samuel Zeter --- libio/Makefile | 1 + libio/fileops.c | 4 ++ libio/genops.c | 2 - libio/tst-bz22796.c | 101 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 libio/tst-bz22796.c diff --git a/libio/Makefile b/libio/Makefile index e143ccdb2c..4b0450afad 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -93,6 +93,7 @@ tests = \ tst-bz22415 \ tst-bz24051 \ tst-bz24153 \ + tst-bz22796 \ tst-bz28828 \ tst-closeall \ tst-eof \ diff --git a/libio/fileops.c b/libio/fileops.c index a59e248142..7921fa0fb2 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -1328,6 +1328,10 @@ _IO_file_xsgetn (FILE *fp, void *data, size_t n) want = n; + /* Set the stream to byte oriented */ + if (fp->_mode == 0) + _IO_fwide (fp, -1); + if (fp->_IO_buf_base == NULL) { /* Maybe we already have a push back pointer. */ diff --git a/libio/genops.c b/libio/genops.c index c3178d9a97..8537a20a92 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -296,8 +296,6 @@ __underflow (FILE *fp) if (_IO_vtable_offset (fp) == 0 && _IO_fwide (fp, -1) != -1) return EOF; - if (fp->_mode == 0) - _IO_fwide (fp, -1); if (_IO_in_put_mode (fp)) if (_IO_switch_to_get_mode (fp) == EOF) return EOF; diff --git a/libio/tst-bz22796.c b/libio/tst-bz22796.c new file mode 100644 index 0000000000..69d2a7f1b7 --- /dev/null +++ b/libio/tst-bz22796.c @@ -0,0 +1,101 @@ +/* Test to ensure stream orientation is set when reading large + buffer sizes with fread (BZ#22796). + + Copyright (C) 2019-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 + +static int fd; +static FILE *fp = NULL; +static char buffer[BUFSIZ] = { 0 }; + +static void +setup (void) +{ + int ret; + + fd = create_temp_file ("bz22796", NULL); + if (fd == -1) + FAIL_EXIT1 ("create_temp_file failed"); + + fp = fdopen (fd, "w"); + if (fp == NULL) + FAIL_EXIT1 ("fopen for file bz22796 returned NULL."); + + /* The stream should have no orientation set yet */ + ret = fwide (fp, 0); + if (ret != 0) + FAIL_EXIT1 ( + "Error: Orientation already set. fwide returned %d.\n", ret); +} + +static int +test_read_large_buffer (void) +{ + int ret; + + setup (); + + /* Read a large amount of bytes */ + fread (buffer, 4, BUFSIZ / 4, fp); + + /* Stream should now be byte orientated */ + ret = fwide (fp, 0); + if (ret != -1) + FAIL_RET ("fread returned %d, expected -1.\n", ret); + + return 0; +} + +static int +test_read_small_buffer (void) +{ + int ret; + + setup (); + + /* Read a small amount of bytes */ + fread (buffer, 4, 4, fp); + + /* Stream should now be byte orientated */ + ret = fwide (fp, 0); + if (ret != -1) + FAIL_RET ("fread returned %d, expected -1.\n", ret); + + return 0; +} + +static int +do_test (void) +{ + int ret = 0; + + ret = test_read_small_buffer (); + ret += test_read_large_buffer (); + + fclose (fp); + + return ret; +} + +#include