From patchwork Sat Jan 11 12:14:53 2025 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: 104575 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 79DD73858406 for ; Sat, 11 Jan 2025 12:29:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 79DD73858406 Authentication-Results: sourceware.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=T9z3+hFw X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from iguana.tulip.relay.mailchannels.net (iguana.tulip.relay.mailchannels.net [23.83.218.253]) by sourceware.org (Postfix) with ESMTPS id 604B33858C48 for ; Sat, 11 Jan 2025 12:28:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 604B33858C48 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 604B33858C48 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.218.253 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1736598533; cv=pass; b=IHJXj5lXgdpSCwD86bzX6xRZSu/oeF+99AWiU+JukyshzV65oySkuEaPllhp9eE3kTCMQ4y2T6KOgtj2jsGt9q5oruT8GayTTsJ6+BpH7z3VdJx6NAJJ8wnAsFG1MiglCteOJ+ZEpBXDqS7BwYVgbW2TU1rpl5vcFGeK0J9hirk= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1736598533; c=relaxed/simple; bh=dEtE1j6LrWs0se0ZdCFNa/PA46duEzaWd1sRQ0oSiOs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pL+dcCQDcwyNPzsjX5Un3VfPk10CFVfUzFq+uiBU0VyNXSgOXfZcOVXpAOP9nsdv05GHXHdgBFXQMCsP4RN5441K3ZXA1ufIeHtXd9kE9JiIX5ATeSzYez5wUA6SHOtrFnQKUfySyuZp7fx1GM9J2wgh+BRJiNsArao8ujxogHA= ARC-Authentication-Results: i=2; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 604B33858C48 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 5A29C2C54A8; Sat, 11 Jan 2025 12:28:52 +0000 (UTC) Received: from pdx1-sub0-mail-a313.dreamhost.com (100-109-45-164.trex-nlb.outbound.svc.cluster.local [100.109.45.164]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id CD3672C4E53; Sat, 11 Jan 2025 12:28:51 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1736598531; a=rsa-sha256; cv=none; b=vNawVSu1OMcugHXcZbXm785ZTEGng7P6YKKDN2X2Czjvme1mM4hqLf2aLthPklFDN4zymg Bvh2ocNHkVARk3GRbvHylXBuaTEKRFQx1D7E5VM/N/wyLFfz7TdiyeoO+TxQL2dlEFHw5K /pEuRp6KCwlEaOjs0zAByLUW1WzH+YB9ci4r7HtA90RSsXuBgqB+ChxVLlpz508esITLSd nzEwq+outP4ReU6iJWwJ0kaWzOHVLm9xKpWiJDWtU99t4Q4m3gjFKp777rdG21UFR/hrRE 0BPTOxHyvLKKqfpXh4lCKOQQlOlgLbINB97CyNvbl8zagubevKegfSBFFLQCdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1736598531; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3zoEMQSrOFMZ2vq1GppHkkBOOHp6HmekGdZEDo25L2w=; b=pvXay/1qKT7jvtIG1YXs3UpA/8qFDGRXsnozF+3A6urR5sgea3FM5Vnvrmjfz5TxsEAdM9 ThnR9pnrbShO/XQsLFQIMrHyYo23KL4nzNlGvyJHl2BzJMduLTr0CJUPpZsotng6QsBnZh zgV+ov5Oxo9SpjQwNGOTLaHMetWTaXYdIyJJBNMYouKVJiUKM0X2ettRlEUO/XzC/RG9to phCut0HadRSvZoALkdZyLmiY9miQnszMIuTcsuENl2TVvfsAgMDkWXiK4MGyXL8Lcb3zl4 yCnofvWNMqaeyd7Ge2LBDBX2wK/plwjsJ7FYTpViGuTc3Gj0GQy2BsarsS8aEg== ARC-Authentication-Results: i=1; rspamd-b5645c5d4-rwdfv; 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-Abortive-Unite: 7ad900501fc906c2_1736598532092_520602498 X-MC-Loop-Signature: 1736598532092:3898507733 X-MC-Ingress-Time: 1736598532092 Received: from pdx1-sub0-mail-a313.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.109.45.164 (trex/7.0.2); Sat, 11 Jan 2025 12:28:52 +0000 Received: from ascii.art.br (ip-191-5-86-51.isp.valenet.com.br [191.5.86.51]) (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-a313.dreamhost.com (Postfix) with ESMTPSA id 4YVd9b2VVqz55; Sat, 11 Jan 2025 04:28:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ascii.art.br; s=dreamhost; t=1736598531; bh=3zoEMQSrOFMZ2vq1GppHkkBOOHp6HmekGdZEDo25L2w=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=T9z3+hFwb4gsYoZe1LwvI6VdaR8hRP/pExA9/k/iUsVxS6B9VJ2kzzw9qoDkXbJuh dYD2DP4UzuRA4yItqmJrL2BYAzSDOsl95Piz42P3BLme29vLpFSXHT+f5G/DZ1BGtK tiJmMXZ0WSkl5tJwQv7t2c6xp4LoN/8LA2ZRActKNogYse0vTTuq1QgLHxm+Co+Acx okmeqVTPTC8Ae/GwolPttSQHQf9cxmus3Sxa6dQRIyDFvB1tpc8M3YnHAL/cI9b8k4 Bwcz7i8C689uFg1CkZedQQsDKaENBM5e7GlfcCb4O8yAbPJjFycFsg246O+Y5eD21F Exz2N/fC7w4gg== From: Tulio Magno Quites Machado Filho To: DJ Delorie , libc-alpha@sourceware.org Cc: fweimer@redhat.com Subject: [PATCH v4] libio: Start to return errors when flushing fwrite's buffer [BZ #29459] Date: Sat, 11 Jan 2025 09:14:53 -0300 Message-ID: <20250111121453.1054012-1-tuliom@ascii.art.br> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-8.3 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 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 v3: - Initialize _total_written. - Update copyright year. - Added a comment elaborating on how _total_written is used in _IO_fwrite. - Added an extra check for the number of bytes read in tst-fwrite-pipe.c. - Fixed a comment about fwrite return in tst-fwrite-pipe.c. Changes since v2: - Fixed an ABI issue with 32-bit libraries. - Added another patch with a FUSE-based test evaluating the integrity of the stream position. Changes since v1: - Properly mentioned the bug being fixed in the commit message. - Changed source code comments as requested. - Integrated the code proposed by Florian. Changes still pending: - FUSE-based test as suggested by Florian. --- 8< --- When an error happens, fwrite is expected to return a value that is less than nmemb. If this error happens while flushing its internal buffer, fwrite is in a complex scenario: all the data might have been written to the buffer, indicating a successful copy, but the buffer is expected to be flushed and it was not. POSIX.1-2024 states the following about errors on fwrite: If an error occurs, the resulting value of the file-position indicator for the stream is unspecified. The fwrite() function shall return the number of elements successfully written, which may be less than nitems if a write error is encountered. With that in mind, this commit modifies _IO_new_file_write in order to return the total number of bytes written via the file pointer. It also modifies fwrite in order to use the new information and return the correct number of bytes written even when sputn returns EOF. Add 2 tests: 1. tst-fwrite-bz29459: This test is based on the reproducer attached to bug 29459. In order to work, it requires to pipe stdout to another process making it hard to reuse test-driver.c. This code is more specific to the issue reported. 2. tst-fwrite-pipe: Recreates the issue by creating a pipe that is shared with a child process. Reuses test-driver.c. Evaluates a more generic scenario. Co-authored-by: Florian Weimer Reviewed-by: DJ Delorie --- libio/bits/types/struct_FILE.h | 9 +- libio/fileops.c | 2 + libio/iofwrite.c | 39 +++++++-- stdio-common/Makefile | 7 ++ stdio-common/tst-fwrite-bz29459.c | 89 ++++++++++++++++++++ stdio-common/tst-fwrite-bz29459.sh | 34 ++++++++ stdio-common/tst-fwrite-pipe.c | 130 +++++++++++++++++++++++++++++ 7 files changed, 304 insertions(+), 6 deletions(-) create mode 100644 stdio-common/tst-fwrite-bz29459.c create mode 100755 stdio-common/tst-fwrite-bz29459.sh create mode 100644 stdio-common/tst-fwrite-pipe.c diff --git a/libio/bits/types/struct_FILE.h b/libio/bits/types/struct_FILE.h index 0e8ac64e36..2012d70681 100644 --- a/libio/bits/types/struct_FILE.h +++ b/libio/bits/types/struct_FILE.h @@ -97,8 +97,15 @@ struct _IO_FILE_complete void *_freeres_buf; struct _IO_FILE **_prevchain; int _mode; +#ifdef __LP64__ + int _unused3; +#endif + __uint64_t _total_written; +#ifndef __LP64__ + int _unused3; +#endif /* Make sure we don't get into trouble again. */ - char _unused2[15 * sizeof (int) - 5 * sizeof (void *)]; + char _unused2[12 * sizeof (int) - 5 * sizeof (void *)]; }; /* These macros are used by bits/stdio.h and internal headers. */ diff --git a/libio/fileops.c b/libio/fileops.c index 775999deb3..12b440b09d 100644 --- a/libio/fileops.c +++ b/libio/fileops.c @@ -113,6 +113,7 @@ _IO_new_file_init_internal (struct _IO_FILE_plus *fp) _IO_link_in (fp); fp->file._fileno = -1; + fp->file._total_written = 0; } /* External version of _IO_new_file_init_internal which switches off @@ -1185,6 +1186,7 @@ _IO_new_file_write (FILE *f, const void *data, ssize_t n) f->_flags |= _IO_ERR_SEEN; break; } + f->_total_written += count; to_do -= count; data = (void *) ((char *) data + count); } diff --git a/libio/iofwrite.c b/libio/iofwrite.c index f94493c6ba..7897c4afaf 100644 --- a/libio/iofwrite.c +++ b/libio/iofwrite.c @@ -36,13 +36,42 @@ _IO_fwrite (const void *buf, size_t size, size_t count, FILE *fp) return 0; _IO_acquire_lock (fp); if (_IO_vtable_offset (fp) != 0 || _IO_fwide (fp, -1) == -1) - written = _IO_sputn (fp, (const char *) buf, request); + { + /* Compute actually written bytes plus pending buffer + contents. */ + uint64_t original_total_written + = fp->_total_written + (fp->_IO_write_ptr - fp->_IO_write_base); + written = _IO_sputn (fp, (const char *) buf, request); + if (written == EOF) + { + /* An error happened and we need to find the appropriate return + value. There 3 possible scenarios: + 1. If the number of bytes written is between 0..[buffer content], + we need to return 0 because none of the bytes from this + request have been written; + 2. If the number of bytes written is between + [buffer content]+1..request-1, that means we managed to write + data requested in this fwrite call; + 3. We might have written all the requested data and got an error + anyway. We can't return success, which means we still have to + return less than request. */ + if (fp->_total_written > original_total_written) + { + written = fp->_total_written - original_total_written; + /* If everything was reported as written and somehow an + error occurred afterwards, avoid reporting success. */ + if (written == request) + --written; + } + else + /* Only already-pending buffer contents was written. */ + written = 0; + } + } _IO_release_lock (fp); /* We have written all of the input in case the return value indicates - this or EOF is returned. The latter is a special case where we - simply did not manage to flush the buffer. But the data is in the - buffer and therefore written as far as fwrite is concerned. */ - if (written == request || written == EOF) + this. */ + if (written == request) return count; else return written / size; diff --git a/stdio-common/Makefile b/stdio-common/Makefile index bbdc1ff709..364ada9a2c 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -262,6 +262,7 @@ tests := \ tst-fwrite \ tst-fwrite-memstrm \ tst-fwrite-overflow \ + tst-fwrite-pipe \ tst-fwrite-ro \ tst-getline \ tst-getline-enomem \ @@ -357,6 +358,7 @@ tests-internal = \ test-srcs = \ $(xprintf-srcs) \ + tst-fwrite-bz29459 \ tst-printf \ tst-printfsz-islongdouble \ tst-unbputc \ @@ -365,6 +367,7 @@ test-srcs = \ ifeq ($(run-built-tests),yes) tests-special += \ $(foreach f,$(xprintf-stems),$(objpfx)$(f).out) \ + $(objpfx)tst-fwrite-bz29459.out \ $(objpfx)tst-printf.out \ $(objpfx)tst-printfsz-islongdouble.out \ $(objpfx)tst-setvbuf1-cmp.out \ @@ -562,6 +565,10 @@ tst-freopen64-6-ENV = \ MALLOC_TRACE=$(objpfx)tst-freopen64-6.mtrace \ LD_PRELOAD=$(common-objpfx)malloc/libc_malloc_debug.so +$(objpfx)tst-fwrite-bz29459.out: tst-fwrite-bz29459.sh $(objpfx)tst-fwrite-bz29459 + $(SHELL) $< $(common-objpfx) '$(test-program-prefix)'; \ + $(evaluate-test) + $(objpfx)tst-unbputc.out: tst-unbputc.sh $(objpfx)tst-unbputc $(SHELL) $< $(common-objpfx) '$(test-program-prefix)'; \ $(evaluate-test) diff --git a/stdio-common/tst-fwrite-bz29459.c b/stdio-common/tst-fwrite-bz29459.c new file mode 100644 index 0000000000..2e8ebc5a08 --- /dev/null +++ b/stdio-common/tst-fwrite-bz29459.c @@ -0,0 +1,89 @@ +/* Test fwrite against bug 29459. + Copyright (C) 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 + . */ + +/* This test is based on the code attached to bug 29459. + It depends on stdout being redirected to a specific process via a script + with the same name. Because of this, we cannot use the features from + test_driver.c. */ + +#include +#include +#include +#include +#include +#include +#include + +/* Usually this test reproduces in a few iterations. However, keep a high + number of iterations in order to avoid return false-positives due to an + overwhelmed/slow system. */ +#define ITERATIONS 5000 + +/* The goal of this test is to use fwrite () on a redirected and closed + stdout. A script will guarantee that stdout is redirected to another + process that closes it during the execution. The process reading from + the pipe must read at least the first line in order to guarantee that + flag _IO_CURRENTLY_PUTTING is set in the write end of the pipe, triggering + important parts of the code that flushes lines from fwrite's internal + buffer. The underlying write () returns EPIPE, which fwrite () must + propagate. */ + +int +main (void) +{ + int i; + size_t rc; + /* Ensure the string we send has a new line because we're dealing + with a lined-buffered stream. */ + const char *s = "hello\n"; + const size_t len = strlen(s); + + /* Ensure that fwrite buffers the output before writing to stdout. */ + setlinebuf(stdout); + /* Ignore SIGPIPE in order to catch the EPIPE returned by the + underlying call to write(). */ + xsignal(SIGPIPE, SIG_IGN); + + for (i = 1; i <= ITERATIONS; i++) + { + /* Keep writing to stdout. The test succeeds if fwrite () returns an + error. */ + if ((rc = fwrite(s, 1, len, stdout)) < len) + { + /* An error happened. Check if ferror () does return an error + and that it is indeed EPIPE. */ + TEST_COMPARE (ferror (stdout), 1); + TEST_COMPARE (errno, EPIPE); + fprintf(stderr, "Success: i=%d. fwrite returned %zu < %zu \ +and errno=EPIPE\n", + i, rc, len); + /* The test succeeded! */ + return 0; + } + else + { + /* fwrite () was able to write all the contents. Check if no errors + have been reported and try again. */ + TEST_COMPARE (ferror (stdout), 0); + TEST_COMPARE (errno, 0); + } + } + + fprintf(stderr, "Error: fwrite did not return an error\n"); + return 1; +} diff --git a/stdio-common/tst-fwrite-bz29459.sh b/stdio-common/tst-fwrite-bz29459.sh new file mode 100755 index 0000000000..164313532b --- /dev/null +++ b/stdio-common/tst-fwrite-bz29459.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# Test fwrite for bug 29459. +# Copyright (C) 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 +# . + +set -e + +common_objpfx=$1; shift +test_program_prefix=$1; shift + +status=0 + +${test_program_prefix} \ + ${common_objpfx}stdio-common/tst-fwrite-bz29459 \ + 2> ${common_objpfx}stdio-common/tst-fwrite-bz29459.out \ + | head -n1 > /dev/null + +grep -q Success ${common_objpfx}stdio-common/tst-fwrite-bz29459.out || status=1 + +exit $status diff --git a/stdio-common/tst-fwrite-pipe.c b/stdio-common/tst-fwrite-pipe.c new file mode 100644 index 0000000000..a6119125b2 --- /dev/null +++ b/stdio-common/tst-fwrite-pipe.c @@ -0,0 +1,130 @@ +/* Test if fwrite returns EPIPE. + Copyright (C) 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 +#include +#include + +/* Usually this test reproduces in a few iterations. However, keep a high + number of iterations in order to avoid return false-positives due to an + overwhelmed/slow system. */ +#define ITERATIONS 5000 + +#define BUFFERSIZE 20 + +/* When the underlying write () fails with EPIPE, fwrite () is expected to + return an error by returning < nmemb and keeping errno=EPIPE. */ + +static int +do_test (void) +{ + int fd[2]; + pid_t p; + FILE *f; + size_t written; + int ret = 1; /* Return failure by default. */ + + /* Try to create a pipe. */ + xpipe (fd); + + p = xfork (); + if (p == 0) + { + char b[BUFFERSIZE]; + size_t bytes; + + /* Read at least the first line from the pipe before closing it. + This is important because it guarantees the file stream will have + flag _IO_CURRENTLY_PUTTING set, which triggers important parts of + the code that flushes lines from fwrite's internal buffer. */ + do { + bytes = read (fd[0], b, BUFFERSIZE); + } while(bytes > 0 && memrchr (b, '\n', bytes) == NULL); + + /* Child closes both ends of the pipe in order to trigger an EPIPE + error on the parent. */ + xclose (fd[0]); + xclose (fd[1]); + + return 0; + } + else + { + /* Ensure the string we send has a new line because we're dealing + with a lined-buffered stream. */ + const char *s = "hello\n"; + size_t len = strlen (s); + int i; + + /* Parent only writes to pipe. + Close the unused read end of the pipe. */ + xclose (fd[0]); + + /* Ignore SIGPIPE in order to catch the EPIPE returned by the + underlying call to write(). */ + xsignal(SIGPIPE, SIG_IGN); + + /* Create a file stream associated with the write end of the pipe. */ + f = fdopen (fd[1], "w"); + TEST_VERIFY_EXIT (f != NULL); + /* Ensure that fwrite buffers the output before writing to the pipe. */ + setlinebuf (f); + + /* Ensure errno is not set before starting. */ + errno = 0; + for (i = 1; i <= ITERATIONS; i++) + { + /* Try to write to the pipe. The first calls are expected to + suceeded until the child process closes the read end. + After that, fwrite () is expected to fail and errno should be + set to EPIPE. */ + written = fwrite (s, 1, len, f); + + if (written == len) + { + TEST_VERIFY_EXIT (ferror (f) == 0); + TEST_VERIFY_EXIT (errno == 0); + } + else + { + /* An error happened. Check if ferror () does return an error + and that it is indeed EPIPE. */ + TEST_COMPARE (ferror (f), 1); + TEST_COMPARE (errno, EPIPE); + /* The test succeeded! Clear the error from the file stream and + return success. */ + clearerr (f); + ret = 0; + break; + } + } + + xfclose (f); + } + + if (ret) + FAIL_RET ("fwrite should have returned an error, but it didn't.\n"); + + return ret; +} + +#include