From 3566cd366fef0955eb76b3be83a682e09cf3496b Mon Sep 17 00:00:00 2001 From: Shuyun Yang Date: Sat, 5 Dec 2026 21:26:48 +0800 Subject: [PATCH] Finalize slurped JSON files when EOF follows a full read buffer (#3686) When the size of a file was an exact multiple of the read buffer, the parser was never told that the input had ended. Pass the final empty read to the parser so that it finalizes at EOF. This makes `--slurpfile` and JSON data imports keep a trailing number or literal and report truncated JSON as an error, regardless of the file size. --- src/jv_file.c | 4 +--- tests/shtest | 13 +++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/jv_file.c b/src/jv_file.c index b43d44a1..15cf652d 100644 --- a/src/jv_file.c +++ b/src/jv_file.c @@ -46,12 +46,10 @@ jv jv_load_file(const char* filename, int raw) { int at_file_start = 1; while (!feof(file) && !ferror(file)) { size_t n = fread(buf, 1, sizeof(buf) - 4, file); - if (n == 0) - continue; char *end = buf + n; int len = 0; - if (jvp_utf8_backtrack(end - 1, buf, &len) && len > 0 && + if (n > 0 && jvp_utf8_backtrack(end - 1, buf, &len) && len > 0 && !feof(file) && !ferror(file)) { n += fread(end, 1, len, file); } diff --git a/tests/shtest b/tests/shtest index ceb1d965..760aa51b 100755 --- a/tests/shtest +++ b/tests/shtest @@ -423,6 +423,19 @@ cat > $d/expected <<'EOF' EOF cmp $d/out $d/expected +# --slurpfile must finalize the parser when EOF follows a full read buffer. +for size in 4095 4096 4097 8192; do + for value in 1 true false null; do + printf '%*s%s' "$((size - ${#value}))" '' "$value" > $d/boundary.json + $VALGRIND $Q $JQ -ne --slurpfile x $d/boundary.json --argjson expected "$value" '$x == [$expected]' + done + for value in '[' '{"x":' '"abc' '1e'; do + printf '%*s%s' "$((size - ${#value}))" '' "$value" > $d/boundary.json + $VALGRIND $Q $JQ -n --slurpfile x $d/boundary.json '$x' > $d/out 2> $d/err && ret=0 || ret=$? + [ "$ret" -eq 2 ] + done +done + # --args, --jsonargs, $ARGS.positional $VALGRIND $JQ -n -c --args '$ARGS.positional' foo bar baz > $d/out printf '["foo","bar","baz"]\n' > $d/expected