summaryrefslogtreecommitdiffhomepage
path: root/lib/fs.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-05-19 15:42:16 +0200
committerJo-Philipp Wich <jo@mein.io>2022-05-20 16:04:14 +0200
commit090b426b00a82cac7b4ea386b077e21ae32f1c17 (patch)
tree7ae656e693e484017cc416cf0de2bd000cc709fb /lib/fs.c
parentf7f6966044a6d705fe60b091c4994931e4c8241c (diff)
fs: avoid input buffering with small limits in fs.readfile()
If the read limit passed to fs.readfile() is smaller than BUFSIZ then disable stdio input buffering to avoid overreading the underlying file. This is useful when reading small amounts of data from special files such as /dev/urandom, where a readfile call limited to 16 bytes might actually read 4096 due to stdio buffering. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib/fs.c')
-rw-r--r--lib/fs.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/lib/fs.c b/lib/fs.c
index 30e5540..b831aee 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -1168,6 +1168,9 @@ uc_fs_readfile(uc_vm_t *vm, size_t nargs)
buf = ucv_stringbuf_new();
+ if (limit > -1 && limit < BUFSIZ)
+ setvbuf(fp, NULL, _IONBF, 0);
+
while (limit != 0) {
blen = 1024;