Berry fix rare crash in json parsing (#18395)

This commit is contained in:
s-hadinger 2023-04-11 23:13:02 +02:00 committed by GitHub
parent f33ccac2df
commit 8ea91b7792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -183,6 +183,10 @@ static const char* parser_string(bvm *vm, const char *json)
} }
} }
be_assert(ch == '"'); be_assert(ch == '"');
/* require the stack to have some free space for the string,
since parsing deeply nested objects might
crash the VM due to insufficient stack space. */
be_stack_require(vm, 1 + BE_STACK_FREE_MIN);
be_pushnstring(vm, buf, cast_int(dst - buf)); be_pushnstring(vm, buf, cast_int(dst - buf));
be_free(vm, buf, len); be_free(vm, buf, len);
return json + 1; /* skip '"' */ return json + 1; /* skip '"' */

View File

@ -34,6 +34,12 @@ assert_load_failed('{"ke: 1}')
assert_load_failed('{"key": 1x}') assert_load_failed('{"key": 1x}')
assert_load_failed('{"key"}') assert_load_failed('{"key"}')
assert_load_failed('{"key": 1, }') assert_load_failed('{"key": 1, }')
# insanely long, nested object
var text = 'null'
for i : 0 .. 200
text = '{"nested":' + text + ', "num": 1, "bool": true, "str": "abc", "n": null, "arr": [1, 2, 3]}'
end
json.load(text) # do nothing, just check that it doesn't crash
# dump tests # dump tests