From d73556c9644fca6dcb053cbfcf933b535a94dd53 Mon Sep 17 00:00:00 2001 From: Lukas Voegl Date: Fri, 30 Oct 2026 12:19:44 +0100 Subject: [PATCH] fs: handle an interrupted read("all") read("all") reads until fread() returns 0 without checking ferror(), so a failed read ends it like EOF: the data is truncated or empty and fs.error() is unset. In uloop hosts such as rpcd, libubox's SIGCHLD handler lacks SA_RESTART, so any exiting child causes this via EINTR. EINTR is now retried. Assisted-By: LLM Signed-off-by: Lukas Voegl --- lib/fs.c | 38 ++++++++++++++++----- tests/custom/06_lib_uloop/14_read_all_eintr | 33 ++++++++++++++++++ tests/custom/99_bugs/64_fs_read_all_error | 17 +++++++++ 3 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 tests/custom/06_lib_uloop/14_read_all_eintr create mode 100644 tests/custom/99_bugs/64_fs_read_all_error diff --git a/lib/fs.c b/lib/fs.c index 2cc88a5c..a028a0fb 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -197,6 +197,7 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, FILE **fp) const char *lstr; int64_t lsize; ssize_t llen; + int err; if (!fp || !*fp) err_return(EBADF); @@ -216,23 +217,44 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, FILE **fp) len = (size_t)llen; } else if (llen == 3 && !strcmp(lstr, "all")) { + clearerr(*fp); + while (true) { + errno = 0; rlen = fread(buf, 1, sizeof(buf), *fp); + err = errno; + + if (rlen > 0) { + tmp = realloc(p, len + rlen); - tmp = realloc(p, len + rlen); + if (!tmp) { + free(p); + err_return(ENOMEM); + } - if (!tmp) { - free(p); - err_return(ENOMEM); + memcpy(tmp + len, buf, rlen); + + p = tmp; + len += rlen; } - memcpy(tmp + len, buf, rlen); + if (rlen == sizeof(buf)) + continue; - p = tmp; - len += rlen; + /* no error means EOF */ + if (!ferror(*fp)) + break; - if (rlen == 0) + clearerr(*fp); + + if (err == EINTR) + continue; + + if (err == EAGAIN || err == EWOULDBLOCK) break; + + free(p); + err_return(err ? err : EIO); } } else if (llen == 1) { diff --git a/tests/custom/06_lib_uloop/14_read_all_eintr b/tests/custom/06_lib_uloop/14_read_all_eintr new file mode 100644 index 00000000..f21429d9 --- /dev/null +++ b/tests/custom/06_lib_uloop/14_read_all_eintr @@ -0,0 +1,33 @@ +read("all") retries a read that a signal interrupted instead of treating it +as end of file, whether or not data arrived before the interruption. + +libubox installs its SIGCHLD handler without SA_RESTART, so in a uloop based +host (rpcd, uhttpd, ...) any child exiting while the VM is blocked in a read +interrupts that read with EINTR. + +-- Testcase -- +{% + import * as uloop from 'uloop'; + import { popen, error } from 'fs'; + + uloop.init(); + + // An unrelated child that exits while we are blocked reading + let other = popen('sleep 1', 'r'); + + let proc = popen('sleep 2; echo payload', 'r'); + + let data = proc.read('all'); + + printf('data: %J\nerror: %J\nexit: %d\n', data, error(), proc.close()); + + other.close(); + uloop.done(); +%} +-- End -- + +-- Expect stdout -- +data: "payload\n" +error: null +exit: 0 +-- End -- diff --git a/tests/custom/99_bugs/64_fs_read_all_error b/tests/custom/99_bugs/64_fs_read_all_error new file mode 100644 index 00000000..dd4f122b --- /dev/null +++ b/tests/custom/99_bugs/64_fs_read_all_error @@ -0,0 +1,17 @@ +read("all") reports a read error as null plus fs.error() instead of +returning an empty string as if it had reached end of file. + +-- Testcase -- +{% + import { open, error } from 'fs'; + + let fp = open('/', 'r'); + + printf('data: %J\nerror: %J\n', fp.read('all'), error()); +%} +-- End -- + +-- Expect stdout -- +data: null +error: "Is a directory" +-- End --