From 0e5a2d43f7c60ffb8a1d770ac7fec0199c2b6ae3 Mon Sep 17 00:00:00 2001 From: huangwenbin Date: Tue, 8 Sep 2026 10:57:26 +0800 Subject: [PATCH] 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. --- apps/json_parse.c | 21 +++++++++++++++++++++ tests/test_json_parse_cli.test | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/apps/json_parse.c b/apps/json_parse.c index 2226117..7887775 100644 --- a/apps/json_parse.c +++ b/apps/json_parse.c @@ -143,6 +143,27 @@ static int parseit(int fd, int (*callback)(struct json_object *)) json_tokener_free(tok); 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); return 0; diff --git a/tests/test_json_parse_cli.test b/tests/test_json_parse_cli.test index c4359ca..9896905 100755 --- a/tests/test_json_parse_cli.test +++ b/tests/test_json_parse_cli.test @@ -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 - _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