Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions tests/custom/99_bugs/65_fs_read_sticky_eof
Original file line number Diff line number Diff line change
@@ -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(<delimiter>) 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 --