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