From 0110d8b56066b5dd1055d9723cfad4d9df240cea Mon Sep 17 00:00:00 2001 From: Joseph Telaak Date: Wed, 11 Nov 2026 03:02:36 -0500 Subject: [PATCH] fs: clear the end-of-file indicator before every read Commit d0750952 ("fs: keep the bytes a short read already delivered") clears the stream's indicators after read(), but read("line"), read("all") and read() still leave the end-of-file indicator set once they reach the end. stdio keeps it set (musl, glibc since 2.28, the BSDs), so every later read of those kinds on the handle returns nothing, even after more data has arrived: a file that grew, a tty in non-canonical mode with VMIN 0, a FIFO that got a new writer. let r = fs.open('/tmp/f', 'r'), w = fs.open('/tmp/f', 'a'); r.read('line'); // "first\n" r.read('line'); // null, at the end of the file w.write('second\n'); w.flush(); r.read('line'); // null, where "second\n" is expected Clear the indicators on entry, so that no read starts from a state an earlier one left behind. Signed-off-by: Joseph Telaak --- lib/fs.c | 5 +++ tests/custom/99_bugs/65_fs_read_sticky_eof | 37 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/custom/99_bugs/65_fs_read_sticky_eof diff --git a/lib/fs.c b/lib/fs.c index 2cc88a5c..e2af848b 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -201,6 +201,11 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, FILE **fp) if (!fp || !*fp) err_return(EBADF); + /* The end-of-file indicator is sticky: left set by an earlier read, it + * makes getline(), getdelim() and fread() return nothing even after more + * data has arrived on a tty, pipe or growing file. */ + clearerr(*fp); + if (ucv_type(limit) == UC_STRING) { lstr = ucv_string_get(limit); llen = ucv_string_length(limit); diff --git a/tests/custom/99_bugs/65_fs_read_sticky_eof b/tests/custom/99_bugs/65_fs_read_sticky_eof new file mode 100644 index 00000000..fcb2a48e --- /dev/null +++ b/tests/custom/99_bugs/65_fs_read_sticky_eof @@ -0,0 +1,37 @@ +A read that reached the end of a file left the stream's end-of-file +indicator set, and stdio keeps it set until it is cleared. Every later +read("line"), read("all") or read() on that handle then returned +nothing, even after more data had been written to the file. + +-- Testcase -- +{% + let fs = require('fs'); + + fs.mkdir("files"); + + for (let mode in [ "line", "all", ",", 64 ]) { + fs.writefile("files/grow.txt", "first\n"); + + let r = fs.open("files/grow.txt", "r"); + let w = fs.open("files/grow.txt", "a"); + + r.read(mode); + r.read(mode); + + w.write("second\n"); + w.flush(); + + printf("%s: %J\n", mode, r.read(mode)); + + r.close(); + w.close(); + } +%} +-- End -- + +-- Expect stdout -- +line: "second\n" +all: "second\n" +,: "second\n" +64: "second\n" +-- End --