]> git.ipfire.org Git - thirdparty/glibc.git/commit
libio: Start to return errors when flushing fwrite's buffer [BZ #29459]
authorTulio Magno Quites Machado Filho <tuliom@redhat.com>
Tue, 28 Jan 2025 18:37:44 +0000 (15:37 -0300)
committerTulio Magno Quites Machado Filho <tuliom@redhat.com>
Tue, 28 Jan 2025 18:37:44 +0000 (15:37 -0300)
commit596a61cf6b51ce2d58b8ca4e1d1f4fdfe1440dbc
tree6e543a49b885e2dfc8de57c84e90137298ba6449
parent45c42b65c29422b773ac94771aa71165e245f8f8
libio: Start to return errors when flushing fwrite's buffer [BZ #29459]

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 <fweimer@redhat.com>
Reviewed-by: DJ Delorie <dj@redhat.com>
libio/bits/types/struct_FILE.h
libio/fileops.c
libio/iofwrite.c
stdio-common/Makefile
stdio-common/tst-fwrite-bz29459.c [new file with mode: 0644]
stdio-common/tst-fwrite-bz29459.sh [new file with mode: 0755]
stdio-common/tst-fwrite-pipe.c [new file with mode: 0644]