From patchwork Thu Sep 5 18:34:29 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: 97191 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 C474A384AB56 for ; Thu, 5 Sep 2024 18:36:59 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from cross.elm.relay.mailchannels.net (cross.elm.relay.mailchannels.net [23.83.212.46]) by sourceware.org (Postfix) with ESMTPS id D701A385C6CE for ; Thu, 5 Sep 2024 18:36:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D701A385C6CE 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 D701A385C6CE Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.212.46 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725561397; cv=pass; b=qh2thn0rYakkHaH0H9rEu7En2deXpCnl1hfy6qYYSU+sdDImDA1lKXmw8qLhdifV0Ak7jBkWte2hoVuY8ej78d7+aBQuS5BUF9t9GCTDi+GQZnvbDBImbk8rP3iVmCocTTZeCpAukM3pT3zoLSDV5Gl6gKUg23tE66mJqJ1GUyM= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725561397; c=relaxed/simple; bh=Gip+5/bMTy91KAm1uiW2LIYb60TQ7Gis/UKR6Euv5HI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=jIdZTawWn88Ns9PGT/o0ijoxmBdvOpyZty+5AcqWNjppphf3wHQOX5t/8/+X/upB5tTtB2WeGOGLGzgHO4kN9i77yCRZqqdztOakmF9TKjeBfnJQssgZ7eO2vzWNb04cUqn8MRsBpObpzIpJ9nk+Lu2i2EHwBRsyjIhxi5/0T1k= 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 B0D335466DE; Thu, 5 Sep 2024 18:36:33 +0000 (UTC) Received: from pdx1-sub0-mail-a203.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 37DBD545A56; Thu, 5 Sep 2024 18:36:33 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1725561393; a=rsa-sha256; cv=none; b=g/n4Z6JXDS3Vb1lEHIgr8TPKvY/rFBwCmXNgR+LclnOw76Jkdv53UYW2kESKEtacQSRhcj oSVPncUp7El0j7mLBS3g/0Z5xs53Ut8S7R7MLI1hRaP9nQXgdCASpaC373DpQGBg97tFgx H/FcQoi1UduBgGkW3E7LaiwPYH+0AvouN2U0nB0rn5y3pDYjmRU2njnDD+3QQtUbLyaWVT Vw4g9q3RH9U5m1rWcehnTY2E24jEC1fPRlvmQEUcfpaHUQdMGvVEVOBjGBj5HbueGGJY78 kCM13dsyl3U+abIHcy2KwxP5ffMz1qIJlvWNFqyQomswCC+vaBLf35IIFEL+Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1725561393; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+O1PNYKVhlIFV+cd83/JcnpkTTEmA4Wg1iyfN30Y7nQ=; b=56MUUj8h9wm/3gefm8jUhve6muYX5lnfa3k/IL8ZISu9xUhlhjGu9Y0Fk4rwAa6iRbeLtb JfspiLdVdRDfm57qkE6EfO9xKQlsx2u+ZHPf9pFFVddeLLLtWhACLTcCk+WsRpT9aO0Fo8 zxG52p289uW+IsREPOP1xGwBR5qwoGudrFNOemRJWgA0yrz0Bjgf6lBWL2ckKZB3XjfJOZ Rm74i7WeceHE/Oniu4462lA0KeP9n6Fh1ikg8vM/5tO15jVho1AT+zwirLIfdAzWQuPqRM w5wc64BK5ehI/NjtxuSqjXKBEreowXeGKaHEUGdkXTeZYr2egkWEcU9KD5WCFw== ARC-Authentication-Results: i=1; rspamd-6bf87dd45-dp2xp; 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-Imminent-Cure: 3ab2299b396e47fd_1725561393448_368714160 X-MC-Loop-Signature: 1725561393448:3520921356 X-MC-Ingress-Time: 1725561393447 Received: from pdx1-sub0-mail-a203.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.107.250.138 (trex/7.0.2); Thu, 05 Sep 2024 18:36:33 +0000 Received: from ascii.art.br (ip-191-5-87-155.isp.valenet.com.br [191.5.87.155]) (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-a203.dreamhost.com (Postfix) with ESMTPSA id 4X07Nw5H1dzN4; Thu, 5 Sep 2024 11:36:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ascii.art.br; s=dreamhost; t=1725561393; bh=+O1PNYKVhlIFV+cd83/JcnpkTTEmA4Wg1iyfN30Y7nQ=; h=From:To:Subject:Date:Content-Transfer-Encoding; b=BhCM/ocVR3ut9D2DSnZfefrSvKIfHQCrcXfac+C9/MK10tjQivs5RZslfAXflSKcN D/tImM6Px8UnJqajMJVh4Rvws+nOotw0oMtYuIAgcHhfZ+8wKOYkJDpcSypAYa4Z48 AMbSMlaOEFAiirWqRAi1xjO0YVOb4WZR+uqdPxNKzCLFwTem9/lWJsS9GtOf7sj3tM homhQ/6U2k4w5z6/baKrDEpAVfa9V1od/bN5/FGYW0fg/GSlkMTlzuJyYK3+Wp9Sp6 ijKUw66RMVJTRo7E+Up3WmJlx+YsSIOBPsZvjHKANyKJDPiZ1cHKjkdHgVKE5/e1BE vSu5uGoqAe8Lg== From: Tulio Magno Quites Machado Filho To: Adhemerval Zanella Netto , libc-alpha@sourceware.org Subject: [PATCH v2] Add a new fwrite test for read-only streams Date: Thu, 5 Sep 2024 15:34:29 -0300 Message-ID: <20240905183429.1330645-1-tuliom@ascii.art.br> X-Mailer: git-send-email 2.46.0 In-Reply-To: <4d904419-fc69-48e4-8245-9aba7cc4ee19@linaro.org> References: <4d904419-fc69-48e4-8245-9aba7cc4ee19@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_ABUSEAT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, 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 Changes since v1: - Rebased the commit on top of the latest code; - Removed implicit checks when using TEST_VERIFY_EXIT (); - Fixed check for return code from create_temp_file (); - Added a check that guarantees the file was not modified; -- >8 -- Ensure that fwrite() behaves correctly even when the stream is read-only. Reviewed-by: Carlos O'Donell --- stdio-common/Makefile | 1 + stdio-common/tst-fwrite-ro.c | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 stdio-common/tst-fwrite-ro.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index d99e0cbfeb..0e18045038 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -223,6 +223,7 @@ tests := \ tst-freopen64-3 \ tst-fseek \ tst-fwrite \ + tst-fwrite-ro \ tst-getline \ tst-getline-enomem \ tst-gets \ diff --git a/stdio-common/tst-fwrite-ro.c b/stdio-common/tst-fwrite-ro.c new file mode 100644 index 0000000000..b15753d4c4 --- /dev/null +++ b/stdio-common/tst-fwrite-ro.c @@ -0,0 +1,69 @@ +/* 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 + +/* A small buffer size is enough to run this test. */ +#define BUFSIZE 4 + +static int +do_test (void) +{ + int fd; + FILE *f; + char buffer[BUFSIZE]; + + /* Create a temporary file and open it in read-only mode. */ + fd = create_temp_file ("tst-fwrite-ro", NULL); + TEST_VERIFY_EXIT (fd != -1); + f = fdopen (fd, "r"); + TEST_VERIFY_EXIT (f != NULL); + + /* 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); + + /* Ensure that we are at the beginning of the file. */ + rewind (f); + /* Check if the file was not modified. That means there is nothing to be + read. EOF must be reached. */ + TEST_COMPARE (fread(buffer, 1, BUFSIZE, f), 0); + TEST_COMPARE (feof (f), 1); + + xfclose (f); + + return 0; +} + +#include