From patchwork Thu Aug 29 14:59:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tulio Magno Quites Machado Filho X-Patchwork-Id: 96699 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 22147385EC2F for ; Thu, 29 Aug 2024 15:00:52 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from weasel.tulip.relay.mailchannels.net (weasel.tulip.relay.mailchannels.net [23.83.218.247]) by sourceware.org (Postfix) with ESMTPS id D71233858402 for ; Thu, 29 Aug 2024 15:00:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D71233858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=ascii.art.br Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ascii.art.br ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D71233858402 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.218.247 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1724943629; cv=pass; b=YBx7BauiVPVD0go+m64xAScc6jQaIEJ5TFxd0dYltf95pmB4H/AuuBSXMc53ytj8Rr9bV2AKvQY7gCF+dLOwvNDIV88Cn6QxbTK6I/AHT5wYQlMhUaa/Li+SBVfLV6YwhfpkIPvjs4RAdbgNUtKT18foL8EY0n0mEBtTCyJbSq4= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1724943629; c=relaxed/simple; bh=6SUX76Ps3yCqZzTsD2xVvozPJ+wGv2hh4ZSaLFMzeHY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BwvqH/FQTaQydR2AqJEwAwWHOqFg3NeR0y5HqpdAlnDXGUXHGMRN7KJZbg3xB8ewuBbhwvdQnaeZFnCn8PKOdm5LPHVnSIndbsvjmBtdIV04keYTqwyViKi35OrGQaIccn2AZpWJRQKE9UfnNQvDglKptjH71GzYhJFyeFdnLZ0= ARC-Authentication-Results: i=2; server2.sourceware.org X-Sender-Id: dreamhost|x-authsender|tuliom@ascii.art.br Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id E6108866B3 for ; Thu, 29 Aug 2024 15:00:25 +0000 (UTC) Received: from pdx1-sub0-mail-a234.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 4AC8D86AAA for ; Thu, 29 Aug 2024 15:00:25 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1724943625; a=rsa-sha256; cv=none; b=le43ne5D1igjSQ29tgHrXSi/vT2PSiRB/YOjFhgPUdZNOrmHid1pfwXWMTEudoi0jtW/MT qVHs3NFAoz5jXrV7cuu5VvKl3+3pu4xnXYIIRyuKcf/9f/SXt3Lu2EtKx8vSf1hW3UiRSM zODmJPvDPmmuKutj8hOA7QDZpHTdpC/xyA27P4KVIIeoMQLtThs6855hlMMlQKqQkdNDv/ PQqeWOIkjvIpAuv6uo7ugrvl9mSctw1FmDZKCvsk77tz+fdlZb7GWH4/X3xYR5eJPLUABI FyuVvr6efhGmmQ9XIxYovApglV6jTlRRWbVHU+LNaczSqnNNxYf183gOHY/lJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1724943625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=3isoy96SsKW2KELR0WVf7K4aJM8qumq9e9qC5epgJzI=; b=AC7Mvi8W1pRbaZQGPiDi1VnC2h0sajDX7jFQshQTrv5Ii0J/wm1CsbA7Q87bBCzkahSlr4 pFYWtRLMEaWIKp82LTmNQROsNlMyfSiZ0dfdORcY0ONtV0A2eizCc8pgJw2OQm5DvsjF9t WgGhlbX8PFgnIQ2PVfqmpFCbuGkfJkYOWzOFR2usmHbNfOzJH4zkbTIns6j7AxTqJgET9s P720vwNAr02OPQUjZOAVwsTFDgVl6x5X8yFuuQ2UJdSszXyOOiOg+eV2jZAAyJd4DYuktW 8Ymq8DOzFEQkRgSqQSNdVYypUqJyk/VoLDN4kmXKmuc3/Rs9bvART0bDgcelWA== ARC-Authentication-Results: i=1; rspamd-5549cdcdbf-6mwdj; auth=pass smtp.auth=dreamhost smtp.mailfrom=tuliom@ascii.art.br X-Sender-Id: dreamhost|x-authsender|tuliom@ascii.art.br X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|tuliom@ascii.art.br X-MailChannels-Auth-Id: dreamhost X-Fumbling-Versed: 570550c52eb61dd3_1724943625715_2918432308 X-MC-Loop-Signature: 1724943625715:3370937432 X-MC-Ingress-Time: 1724943625714 Received: from pdx1-sub0-mail-a234.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.96.92.204 (trex/7.0.2); Thu, 29 Aug 2024 15:00:25 +0000 Received: from ascii.art.br (ip-191-5-85-255.isp.valenet.com.br [191.5.85.255]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: tuliom@ascii.art.br) by pdx1-sub0-mail-a234.dreamhost.com (Postfix) with ESMTPSA id 4Wvkwm5FYDz6t for ; Thu, 29 Aug 2024 08:00:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ascii.art.br; s=dreamhost; t=1724943625; bh=3isoy96SsKW2KELR0WVf7K4aJM8qumq9e9qC5epgJzI=; h=From:To:Subject:Date:Content-Transfer-Encoding; b=Yhl7yZw4g94SH4Eo47qZdNFkK8dvEeZ+ZzLQ1nGAxVJEcUvO0vhl9rxgvsk7XoWcN r/izxIOTp8yvMdXJCdzvl9ka9fkD7PjddOgrLyG2CpgG+V4e0Y2uW+fTdgMKQDmExq P0lVlNufvUl5N/kLtDmlhJqH/00n0hC6TxyIxbiwTDwN0mHWePCdFF92poPdOpWL6x 83joNrfkTeP9EmKgUGK2YAj3nvKnplEOUZl+CUlVcoW1H/Bwa6yHb9jTpy5HCg8zra v6TgPkhdvibIUep/ajXUXW1LDHbFvHXP2ze8sKrAzthG/9rRK7P36rFTI+5cP3AjpW CSlICR02l6PSQ== From: Tulio Magno Quites Machado Filho To: libc-alpha@sourceware.org Subject: [PATCH] Add a new fwrite test for read-only streams Date: Thu, 29 Aug 2024 11:59:58 -0300 Message-ID: <20240829145958.853796-1-tuliom@ascii.art.br> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 From: Tulio Magno Quites Machado Filho Ensure that fwrite() behaves correctly even when the stream is read-only. --- stdio-common/Makefile | 1 + stdio-common/tst-fwrite-ro.c | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 stdio-common/tst-fwrite-ro.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index e4f0146d2c7..315152f30df 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -218,6 +218,7 @@ tests := \ tst-fphex-wide \ tst-fseek \ tst-fwrite \ + tst-fwrite-ro \ tst-gets \ tst-grouping \ tst-grouping2 \ diff --git a/stdio-common/tst-fwrite-ro.c b/stdio-common/tst-fwrite-ro.c new file mode 100644 index 00000000000..cd67bf774ee --- /dev/null +++ b/stdio-common/tst-fwrite-ro.c @@ -0,0 +1,56 @@ +/* Test fwrite on a read-only stream. + Copyright (C) 2024 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 +do_test (void) +{ + int fd; + FILE *f; + + /* Create a temporary file and open it in read-only mode. */ + TEST_VERIFY_EXIT ((fd = create_temp_file ("tst-fwrite-ro", NULL))); + TEST_VERIFY_EXIT ((f = fdopen (fd, "r"))); + + /* Try to write to the temporary file with nmemb = 0, then check that + fwrite returns 0. No errors are expected from this. */ + TEST_COMPARE (fwrite ("a", 1, 0, f), 0); + TEST_COMPARE (ferror (f), 0); + + /* Try to write to the temporary file with size = 0, then check that + fwrite returns 0. No errors are expected from this. */ + TEST_COMPARE (fwrite ("a", 0, 1, f), 0); + TEST_COMPARE (ferror (f), 0); + + /* Try to write a single byte to the temporary file, then check that + fwrite returns 0. Check if an error was reported. */ + TEST_COMPARE (fwrite ("a", 1, 1, f), 0); + TEST_COMPARE (ferror (f), 1); + + clearerr (f); + xfclose (f); + + return 0; +} + +#include