From patchwork Fri Sep 6 18:04: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: 1982009 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=idv/3cpe; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4X0kfJ3RYMz1y1D for ; Sat, 7 Sep 2024 04:05:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B6B80384A41C for ; Fri, 6 Sep 2024 18:05:10 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from aye.elm.relay.mailchannels.net (aye.elm.relay.mailchannels.net [23.83.212.6]) by sourceware.org (Postfix) with ESMTPS id 2B809385B505 for ; Fri, 6 Sep 2024 18:04:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2B809385B505 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 2B809385B505 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.212.6 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725645893; cv=pass; b=tgygkVC4B5WSZPkuM939Ip7jl5RfNCLTxqFjCRzKN6WpNW4V+rOlRM+NkLiqiahOPuf2JQ+B6k34gHV0e0kPsDTylIGyYmUC1LtxdI+l08olSJuideMMFdnha/RIpu7u2Vtj6Ek1xHTbh97YS3XyGbd5Eas3cAcuSla4OHieX1Q= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1725645893; c=relaxed/simple; bh=s9HWao1m+Qwc0Mf9DN/8NNaeN/cENEWnNpZ20xKYkps=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QUwpyb5+AHGp86k59PtRCTHSL/nWXeAiJBOPiLI2DYLkEriKfhJJGRuyuu5go1IExFIirJ/YNclUvleW+kRZvaBxiCrnoZtTRs9XllDLKdZhHymTUAflWF/vyH39fEyQXIyF5MXqNRCwTdCUwtfRCcF7wPWUIqouCjtuC2UYzd8= 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 2A6C88739E for ; Fri, 6 Sep 2024 18:04:50 +0000 (UTC) Received: from pdx1-sub0-mail-a230.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id CE7FB86BBE for ; Fri, 6 Sep 2024 18:04:49 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1725645889; a=rsa-sha256; cv=none; b=91K94Gqs0YTG9I+6rjKQTvw86NnIpy2tIeJJSou6P0k0soUVleWCksBAEbyN1yxgPY8TwV /PmfzsSg0+L52zt+9UfVkOlpQiVH3vWrCFrwmre/mT2PtF1Tofh0OlAmbbUfKTKn4E/VZk /E8CxmU6ufKbLxfu6OpbPWT/mOOZqMhtSSc14FAeeFJN7FTd2/K0qyTi/ebKlborW4lErD lm9+qzcLDCQiVhINNIlSJelylNlXZ0IS87RMrQw0tWGBtsfWSEuTy980yqy2QkrNWJHRbV NrKEBTZvkpX3v6Gn04I3QfpeKtFjUkmgh68GH3LRyLv9el3f7JBP9SYb4mvgBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1725645889; 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=klWoTnz6UAzfyR47Pt+WSS+RrqN7GvQAOYCvFxEjFl0=; b=UA9QymgCa0yfd4zV0GzuxradouFB6izn0ydOQRwiTMBpxN2RvjYPjpu1ZogdeK2Geuw2HX VmHOb6K7YlcK3MNPX5DgEW0kFuxDeRXtliKuRyBfOjzxO6eCnDS/Rd1wUURNvaYuufqXbM Udi0qO2qWCOwMwnNtnMfE0tgFXr+Nu4fdDsASgCUoR47rHWWIYKvivps727ojrAioW8R0n JiUsJPnNSpkGV4x34rrINmi4hebD72LkxR9QarVQvjKe3EeU1flOUBoZ14iZF0YoBA6ROG n75xbtUSssPPIz5R2HJc7asaLJjrK0x6Bj0S5M6YRT6xr2zgx5/tL4PkeOmkPQ== ARC-Authentication-Results: i=1; rspamd-85dcd79db4-9s56m; 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-Battle-Spicy: 2d6ab32c13ee50a3_1725645890068_2145380909 X-MC-Loop-Signature: 1725645890068:1636395963 X-MC-Ingress-Time: 1725645890068 Received: from pdx1-sub0-mail-a230.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.99.25.74 (trex/7.0.2); Fri, 06 Sep 2024 18:04:50 +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-a230.dreamhost.com (Postfix) with ESMTPSA id 4X0kds2cYsz9t for ; Fri, 6 Sep 2024 11:04:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ascii.art.br; s=dreamhost; t=1725645889; bh=klWoTnz6UAzfyR47Pt+WSS+RrqN7GvQAOYCvFxEjFl0=; h=From:To:Subject:Date:Content-Transfer-Encoding; b=idv/3cpejL+UOkXpEZ+wSwUQuSqTe6G8jiXsdersYCNShF7bBmkbkzzcqk5dOTce/ I3c8LmsJVHcc04Hbm8EjRAuqGKVrf6/cqjq5HmGEGdwMLKKlyt+9wRA0Ds3XBBphid FZXq3q5g/h5Y/gzFwEMOgPCer1YAkYcz6e/Fu6XpzRQ8Cdm7TEO6N1NOVnRuuDrcDe Q/c0mI75zZnKlA9Tbk41e99gjjzA5yekz/H3XckVQyq201b+WCKMBCsqNqEkofw8yL VbBr6O0ZPtvCZpxpMubGVcIIL2UpwOvL6B+vNJCpROzlq2Lm2kEQVQ1KP9HcD2ORJM lpzUUhxbIxllw== From: Tulio Magno Quites Machado Filho To: libc-alpha@sourceware.org Subject: [PATCH v2] Add a new fwrite test for memory streams Date: Fri, 6 Sep 2024 15:04:29 -0300 Message-ID: <20240906180429.1463544-1-tuliom@ascii.art.br> X-Mailer: git-send-email 2.46.0 In-Reply-To: <874j6t7wvu.fsf@ascii.art.br> References: <874j6t7wvu.fsf@ascii.art.br> 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~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 | 177 ++++++++++++++++++++++++++++++ 2 files changed, 178 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..a502d9e21e --- /dev/null +++ b/stdio-common/tst-fwrite-memstrm.c @@ -0,0 +1,177 @@ +/* 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. */ + out = malloc(2); + TEST_VERIFY_EXIT (out != NULL); + + /* Try to open the allocated buffer as a read-only stream. */ + f = fmemopen (out, 2, "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); + 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. */ + out = malloc(RWBUF_SIZE); + TEST_VERIFY_EXIT (out != NULL); + 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. */ + expected_out = malloc(RWBUF_SIZE); + TEST_VERIFY_EXIT (expected_out != NULL); + if (expected_bytes > 0) + { + memcpy (expected_out, in, expected_bytes); + expected_out[expected_bytes] = 0; + memset (expected_out + expected_bytes + 1, KNOWN_BYTE, + RWBUF_SIZE - expected_bytes - 1); + } + else + { + /* No changes to the output are expected. */ + memset (expected_out, KNOWN_BYTE, RWBUF_SIZE); + } + + /* Try to open the allocated buffer as a read-write stream. */ + f = fmemopen (out, RWBUF_SIZE, "w"); + TEST_VERIFY_EXIT (f != NULL); + + /* 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); + + in = malloc(RWBUF_SIZE); + TEST_VERIFY_EXIT (in != NULL); + 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