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.
This commit is contained in:
1 parent
629622d099
commit
3566cd366f
2 files changed
+14
-3
No files matched your search
+1
-3
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user