casaroli commented on code in PR #22179:
URL: https://github.com/apache/nuttx/pull/22179#discussion_r4665494878
##########
libs/libc/stdio/lib_libfflush.c:
##########
@@ -36,6 +36,33 @@
#include "libc.h"
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: lib_fflush_rdflush
+ *
+ * Description:
+ * Discard the read-ahead data of a stream and set the file offset of
+ * the underlying file to the stream position. A stream that cannot
+ * seek (a pipe or a terminal) has no offset to restore, which is not
+ * an error.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_STDIO_DISABLE_BUFFERING
+static ssize_t lib_fflush_rdflush(FAR FILE *stream)
+{
+ if (lib_rdflush_unlocked(stream) < 0 && get_errno() != ESPIPE)
Review Comment:
lib_rdflush_unlocked() empties the buffer before it calls lseek(), so when
lseek() fails with ESPIPE the read-ahead data is already gone. lib_flushall()
flushes stdin too, so fflush(NULL) now drops the buffered input of a stdin that
is a pipe or a terminal. POSIX defines the flush of an input stream only when
"the file is one capable of seeking", and fflush(NULL) acts only "on all
streams for which the behavior is defined above". On qemu-armv8a:knsh with
stdin on a FIFO holding "xyz": getchar(), fflush(NULL), getchar() gives x, y on
master and x, then EOF with this PR.
Fix: check that the stream can seek (for example lseek(fd, 0, SEEK_CUR)
succeeds) before you discard its read data, and leave a stream that cannot seek
unchanged.
##########
libs/libc/stdio/lib_libfflush.c:
##########
@@ -90,11 +120,13 @@ ssize_t lib_fflush_unlocked(FAR FILE *stream)
if (stream->fs_bufread != stream->fs_bufstart)
{
- /* The buffer holds read data... just return zero meaning "no bytes
- * remaining in the buffer."
+ /* The buffer holds read data. Discard it, including any
+ * characters pushed back with ungetc(), and set the file offset
+ * to the stream position. There are no bytes remaining in the
+ * buffer.
*/
- return 0;
+ return lib_fflush_rdflush(stream);
Review Comment:
lib_fread_unlocked() calls lib_wrflush_unlocked() before every read, and for
a stream opened for reading and writing that ends here. With read data in the
buffer this branch now discards the buffer and seeks back, so every fgetc()
after the first on an "r+" or "w+" stream reads the file again, and on a stream
that cannot seek (a FIFO or a terminal opened "r+") the read-ahead bytes are
lost. On qemu-armv8a:knsh, fdopen() of a FIFO with "abcdef" as "r+", then two
fgetc(): master gives a, b; with this PR the second fgetc() returns EOF.
Fix: discard the read data only for an explicit fflush() of the stream; keep
`return 0` in this branch when lib_wrflush_unlocked() is the caller (for
example do the read-side flush in fflush() before it calls
lib_fflush_unlocked()).
##########
libs/libc/stdio/lib_freopen.c:
##########
@@ -192,6 +197,10 @@ FAR FILE *freopen(FAR const char *path, FAR const char
*mode,
return NULL;
}
+ /* Set the new file mode for the file stream */
+
+ stream->fs_oflags = oflags;
Review Comment:
freopen(NULL, mode, stream) does not flush the stream, and now the stream
gets the new mode. A write stream with buffered data changed to "r" is then
flushed as a read stream by fclose(), and the data is never written. POSIX:
"The freopen() function shall first attempt to flush the stream associated with
stream as if by a call to fflush(stream)." On qemu-armv8a:knsh, fdopen() of a
FIFO as "w", fputs("hello"), freopen(NULL, "r"), fclose(): master writes the 5
bytes, this PR writes none.
Fix: call lib_fflush() on the stream before fcntl(F_SETFL) in the path ==
NULL branch, as the other branch does.
##########
libs/libc/stdio/Kconfig:
##########
@@ -29,7 +29,7 @@ menu "Standard C I/O"
config STDIO_DISABLE_BUFFERING
bool "Disable STDIO Buffering"
depends on FILE_STREAM
- default n
+ default DEFAULT_SMALL
Review Comment:
This default turns stdio buffering off in 53 in-tree defconfigs
(DEFAULT_SMALL with FILE_STREAM, for example olimexino-stm32:nsh and
stm32f103-minimum:*), and hides CONFIG_STDIO_BUFFER_SIZE=255 of
nucleo-f302r8:can. Also, with STDIO_LINEBUFFER, fopen() and fdopen() streams
that are not terminals are now fully buffered (they were line buffered). The
Impact section says "No impact on the build system".
Fix: list both changes in Impact.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]