mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-07 06:43:00 +03:00
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>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user