diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-11-29 10:17:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-29 10:17:46 +0100 |
commit | fd237733acb7520332dfe9b36795d0414057a07e (patch) | |
tree | 196a0710a292e5b3bfd1266a1439d764b79756d7 /lib.c | |
parent | eef83d3e849743911b29133e6ef873f7e0d34c1c (diff) | |
parent | 394e901bc18196cc72d614c69cc8276c618e9b3e (diff) |
Merge pull request #128 from jow-/lib-json-improve-trailing-garbage-handling
lib: uc_json(): accept trailing whitespace when parsing strings
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -2354,20 +2354,28 @@ static struct json_tokener * uc_json_from_string(uc_vm_t *vm, uc_value_t *str, json_object **jso) { struct json_tokener *tok = xjs_new_tokener(); + size_t i; + char *p; /* NB: the len + 1 here is intentional to pass the terminating \0 byte * to the json-c parser. This is required to work-around upstream * issue #681 <https://github.com/json-c/json-c/issues/681> */ *jso = json_tokener_parse_ex(tok, ucv_string_get(str), ucv_string_length(str) + 1); - if (json_tokener_get_error(tok) == json_tokener_success && - json_tokener_get_parse_end(tok) < ucv_string_length(str)) { - uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, - "Trailing garbage after JSON data"); + if (json_tokener_get_error(tok) == json_tokener_success) { + p = ucv_string_get(str); - json_tokener_free(tok); + for (i = json_tokener_get_parse_end(tok); i < ucv_string_length(str); i++) { + if (!isspace(p[i])) { + uc_vm_raise_exception(vm, EXCEPTION_SYNTAX, + "Trailing garbage after JSON data"); - return NULL; + + json_tokener_free(tok); + + return NULL; + } + } } return tok; |