From patchwork Thu Sep 5 18:56:25 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: 1981387 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ascii.art.br header.i=@ascii.art.br header.a=rsa-sha256 header.s=dreamhost header.b=lTaahWfn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X07rc62gYz1yXY for ; Fri, 6 Sep 2024 04:57:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AE402384AB69 for ; Thu, 5 Sep 2024 18:57:02 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from gainsboro.ash.relay.mailchannels.net (gainsboro.ash.relay.mailchannels.net [23.83.222.65]) by sourceware.org (Postfix) with ESMTPS id 7D659385842D for ; Thu, 5 Sep 2024 18:56:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D659385842D 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 7D659385842D Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.222.65 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725562609; cv=pass; b=h1g75TiJ+W7w7WQZ2SkLOaejKNKg+4jAAda0PIreWP80GC0BTtWu0kttErTResK4LA8Gu6JmQ6zU3ftzSbhdIIx6qvcivrzNI0AYxrGG/tRqRkxKEVF1ACj4vkQ77/nTuiiMUkgarFbx+zHNUBnPGVCWpxySJ5842gIsc0Y/ufg= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725562609; c=relaxed/simple; bh=05+ei8eTfq4FCA5ytjRjff4xbESy5pXSCFm5Kxm4+e4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nOXm1vDm66EwlZqRLMdO8NTybT/eb4CTwVEanC1kS593AqI2bj6A1LT/4+wi2IAzB0p4WrTvvtDXtoCXRoHABI1O2xqQvOHkeeBBwF91Xb5drFmER70NELOyaTqUSXDWME3+cKxtSehT5mWFWtZgUIk+qyZ4V1ZTTC9Cd8UL9hI= 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 4A7E185A18 for ; Thu, 5 Sep 2024 18:56:44 +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 F0DBD85866 for ; Thu, 5 Sep 2024 18:56:43 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1725562604; a=rsa-sha256; cv=none; b=nJS+3Xobf3L5/DncsztxGZM+uO9MIF+rTaEh5KMoKsZl2ZyVnvscIxah4M99xQu8dko+Nh 6d0W0ebpq9Y0iDgaJfTq3ifQQ735HTHf6eKiqmLaDqYfJwm6fFPSCV2soKeEpX09CQZNI7 p5iqK/fRt3wa99vw6L5ncSI0vBX7/eghgDyrszLs8iXvRYuqHbQXgjETmeGbhEQ4a49V5G TiFs2/qKX5ygNNK75n9LKd/Pwcwr8D+VD8/ouifv/dUoAj5FxTtsJzXDy7aEnlQTFkGBeS 4pIhNpippTULLbq9oMAP1gCVjVfsEPyC1iD1gKXo9syoUGluSy8p/CZt5oie5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1725562604; 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=VnYniPIF6wA/efKGPhu9+O0r6UZ9jRG+b7jgRrv+yhU=; b=BtD4NZPgemfEvqSiv04KlYoGnMHhasJD1nJ8cWX8zNJ/5r5S8Lhg5EuRvYH4kCs1CwKDOA XWG4Nphq+6K+wmCTFtfYV5tAltpNSwRMxwmVqJQDG7szdJRDiMJuJXQnvykhAsDRIkrQgN qhZb2CURodrLyP/+6ftIJ/rW4nS2ddtK4sxdAsvVSU8dicB4KvyLV9Erolceimo0Sfpb4u mTRN/2oVCuTBfpL+Klvg7nMlq0foOPNns65O+bhkMofFMxMaIZ9c1vl+utUt+8v5ydbGag 4sbOMw4hZTe7fHg5Up4QtZLBSS/PbRFMjadwFeNI81PCCcO6glpyBuz1jF+c3g== ARC-Authentication-Results: i=1; rspamd-85dcd79db4-5dqxp; 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-Inform-Celery: 7b729156233904f8_1725562604201_2389700680 X-MC-Loop-Signature: 1725562604201:4090126596 X-MC-Ingress-Time: 1725562604201 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.112.250.152 (trex/7.0.2); Thu, 05 Sep 2024 18:56:44 +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 4X07rC3cnPz74 for ; Thu, 5 Sep 2024 11:56:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ascii.art.br; s=dreamhost; t=1725562603; bh=VnYniPIF6wA/efKGPhu9+O0r6UZ9jRG+b7jgRrv+yhU=; h=From:To:Subject:Date:Content-Transfer-Encoding; b=lTaahWfnlSWg3yLv913hvg2NuNZa+2yMk9J00ydqEhBUUWbazGYvCaHpG3LtsLP8k V5QxIWxSsdJCfj9Z09K/g2TyYlPZX2NVaUQnU64LWWeUS0j4gfXhAgK/CWcelM7mw/ kxI1G3Wd5gidWfHNuygG0u3Uuch8xAhOUS5MBUJxeLdAQeetkUQB83q8YLRyj70k+2 LntVfQr2UAysFYftZeLZIaQHVYJlVF/YiYgGM6ZgafAjgdrpjmWnSPsbOVTdbbyA9j YEOTdT7eltmVfX7FiVxT5qFuRCnmAmmYpheqONDS0wejj+w6sXWk4Egm9C4GM+1CSS 0CniuDz48yaew== From: Tulio Magno Quites Machado Filho To: libc-alpha@sourceware.org Subject: [PATCH] Add a new fwrite test for memory streams Date: Thu, 5 Sep 2024 15:56:25 -0300 Message-ID: <20240905185625.2726636-1-tuliom@ascii.art.br> X-Mailer: git-send-email 2.46.0 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_H3, RCVD_IN_MSPIKE_WL, 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~incoming=patchwork.ozlabs.org@sourceware.org From: Tulio Magno Quites Machado Filho Ensure that fwrite() behaves correctly when using memory streams. Reviewed-by: Carlos O'Donell --- stdio-common/Makefile | 1 + stdio-common/tst-fwrite-memstrm.c | 171 ++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 stdio-common/tst-fwrite-memstrm.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index d99e0cbfeb..57e8cfb499 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -223,6 +223,7 @@ tests := \ tst-freopen64-3 \ tst-fseek \ tst-fwrite \ + tst-fwrite-memstrm \ tst-getline \ tst-getline-enomem \ tst-gets \ diff --git a/stdio-common/tst-fwrite-memstrm.c b/stdio-common/tst-fwrite-memstrm.c new file mode 100644 index 0000000000..840da07786 --- /dev/null +++ b/stdio-common/tst-fwrite-memstrm.c @@ -0,0 +1,171 @@ +/* Test fwrite on a memory 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 +#include +#include + +void +test_ro (void) +{ + FILE *f; + char *out; + + /* Try to allocate a small buffer for this test. */ + TEST_VERIFY_EXIT ((out = malloc(2))); + + /* Try to open the allocated buffer as a read-only stream. */ + TEST_VERIFY_EXIT ((f = fmemopen (out, 2, "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); + free (out); +} + +/* Length of the output buffer in bytes. */ +#define RWBUF_SIZE 16 * 1024 +/* Maximum number of bytes to be written in output buffer. The rest will be + used to check against overflow. */ +#define RWBUF_SIZE_WRITABLE RWBUF_SIZE-2048 + +/* Use the following byte to identify areas that should have not been + modified. */ +#define KNOWN_BYTE 0xaa + +void +test_one_rw (const char *in, size_t size, size_t nmemb, + size_t expected_ret) +{ + FILE *f; + char *out, *expected_out; + /* Total number of bytes expected to be written. */ + size_t expected_bytes = size * nmemb; + + printf("Testing with size = %zd, nmemb = %zd\n", size, nmemb); + + TEST_VERIFY_EXIT (expected_ret <= RWBUF_SIZE_WRITABLE); + TEST_VERIFY_EXIT (expected_bytes <= RWBUF_SIZE_WRITABLE); + + /* Try to allocate a buffer for this test and initialize it with + known contents. */ + TEST_VERIFY_EXIT ((out = malloc(RWBUF_SIZE))); + TEST_VERIFY_EXIT (memset (out, KNOWN_BYTE, RWBUF_SIZE)); + + /* Try to allocate a buffer and fill it with the contents that are expected + to be in memory after flushing/closing the memory stream. */ + TEST_VERIFY_EXIT ((expected_out = malloc(RWBUF_SIZE))); + if (expected_bytes > 0) + { + TEST_VERIFY_EXIT (memcpy (expected_out, in, expected_bytes)); + expected_out[expected_bytes] = 0; + TEST_VERIFY_EXIT (memset (expected_out + expected_bytes + 1, + KNOWN_BYTE, RWBUF_SIZE - expected_bytes - 1)); + } + else + { + /* No changes to the output are expected. */ + TEST_VERIFY_EXIT (memset (expected_out, KNOWN_BYTE, RWBUF_SIZE)); + } + + /* Try to open the allocated buffer as a read-write stream. */ + TEST_VERIFY_EXIT ((f = fmemopen (out, RWBUF_SIZE, "w"))); + + /* Try to write to the memory stream. Check if fwrite() returns the + expected value. No errors are expected. */ + TEST_COMPARE (fwrite (in, size, nmemb, f), expected_ret); + TEST_COMPARE (ferror (f), 0); + + xfclose (f); + + /* Ensure the output has the expected contents. */ + TEST_COMPARE (memcmp (out, expected_out, expected_bytes), 0); + + free (expected_out); + free (out); +} + +void +test_rw (void) +{ + char * in; + int i, j; + size_t size[] = {1, 8, 11, 16, 17, 0}; + size_t nmemb[] = {32, 83, 278, 709, 4097, RWBUF_SIZE / 2, + RWBUF_SIZE_WRITABLE, 0}; + size_t n; + + /* Try to write to the temporary file with nmemb = 0, then check that + fwrite returns 0; */ + test_one_rw ("a", 1, 0, 0); + + /* Try to write to the temporary file with size = 0, then check that + fwrite returns 0; */ + test_one_rw ("a", 0, 1, 0); + + /* Try to write a single byte to the temporary file, then check that + fwrite returns 1; */ + test_one_rw ("a", 1, 2, 2); + + TEST_VERIFY_EXIT ((in = malloc(RWBUF_SIZE))); + for (i = 0; i < RWBUF_SIZE / 2; i++) + in[i] = i % 0xff; + + /* Test with all posibilities of size[] x nmemb[]. */ + for (i = 0; nmemb[i] != 0; i++) + { + for (j = 0; size[j] != 0; j++) + { + n = nmemb[i] / size[j]; + test_one_rw (in, size[j], n, n); + } + /* Run the test with a single item of maximum size. */ + test_one_rw (in, nmemb[i], 1, 1); + } + + free (in); +} + +static int +do_test (void) +{ + test_ro (); + test_rw (); + + return 0; +} + +#include