json_parse returned success when input ended while the tokener was

still waiting for more data, and also after read() failures.

Finalize the tokener at EOF with the terminating NUL so valid scalar
values can complete, while rejecting genuinely truncated input.

Also propagate read errors to the caller and add CLI regression tests.
This commit is contained in:
huangwenbin committed 2026-09-14 15:23:04 +08:00
1 parent a954bda619
commit 0e5a2d43f7
2 files changed
+39

No files matched your search

+21
View File
@@ -143,6 +143,27 @@ static int parseit(int fd, int (*callback)(struct json_object *))
json_tokener_free(tok); json_tokener_free(tok);
return 1; return 1;
} }
if (json_tokener_get_error(tok) == json_tokener_continue)
{
obj = json_tokener_parse_ex(tok, "", 1);
if (obj != NULL)
{
int cb_ret = callback(obj);
json_object_put(obj);
if (cb_ret != 0)
{
json_tokener_free(tok);
return 1;
}
}
if (json_tokener_get_error(tok) != json_tokener_success)
{
fprintf(stderr, "Failed at offset %lu: unexpected end of data\n",
(unsigned long)total_read);
json_tokener_free(tok);
return 1;
}
}
json_tokener_free(tok); json_tokener_free(tok);
return 0; return 0;
+18
View File
@@ -18,4 +18,22 @@ TESTNAME="${TESTNAME%.test}"
( cat file1.dat ; cat file2.dat ) | run_output_test -o "${TESTNAME}" --exit 1 ../apps/json_parse -u -N -n - ( cat file1.dat ; cat file2.dat ) | run_output_test -o "${TESTNAME}" --exit 1 ../apps/json_parse -u -N -n -
_err=$? _err=$?
printf '{' | "${top_builddir}/../apps/json_parse" -N -n - > /dev/null 2>&1
if test $? -eq 0; then
echo "ERROR: json_parse accepted incomplete input" 1>&2
_err=1
fi
printf '123' | "${top_builddir}/../apps/json_parse" -N -n - > /dev/null 2>&1
if test $? -ne 0; then
echo "ERROR: json_parse rejected valid EOF-terminated scalar input" 1>&2
_err=1
fi
"${top_builddir}/../apps/json_parse" -N -n - <&- > /dev/null 2>&1
if test $? -eq 0; then
echo "ERROR: json_parse returned success after read failure" 1>&2
_err=1
fi
exit $_err exit $_err