mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
eval/decode: Make sure that blank input does not crash Neovim
This commit is contained in:
parent
394830631f
commit
224d7df630
@ -223,6 +223,15 @@ int json_decode_string(const char *const buf, const size_t len,
|
|||||||
typval_T *const rettv)
|
typval_T *const rettv)
|
||||||
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
|
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
|
||||||
{
|
{
|
||||||
|
const char *p = buf;
|
||||||
|
const char *const e = buf + len;
|
||||||
|
while (p < e && (*p == ' ' || *p == '\t' || *p == '\n')) {
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
if (p == e) {
|
||||||
|
EMSG(_("E474: Attempt to decode a blank string"));
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
vimconv_T conv = { .vc_type = CONV_NONE };
|
vimconv_T conv = { .vc_type = CONV_NONE };
|
||||||
convert_setup(&conv, (char_u *) "utf-8", p_enc);
|
convert_setup(&conv, (char_u *) "utf-8", p_enc);
|
||||||
conv.vc_fail = true;
|
conv.vc_fail = true;
|
||||||
@ -232,11 +241,9 @@ int json_decode_string(const char *const buf, const size_t len,
|
|||||||
ContainerStack container_stack;
|
ContainerStack container_stack;
|
||||||
kv_init(container_stack);
|
kv_init(container_stack);
|
||||||
rettv->v_type = VAR_UNKNOWN;
|
rettv->v_type = VAR_UNKNOWN;
|
||||||
const char *const e = buf + len;
|
|
||||||
bool didcomma = false;
|
bool didcomma = false;
|
||||||
bool didcolon = false;
|
bool didcolon = false;
|
||||||
bool next_map_special = false;
|
bool next_map_special = false;
|
||||||
const char *p = buf;
|
|
||||||
for (; p < e; p++) {
|
for (; p < e; p++) {
|
||||||
json_decode_string_cycle_start:
|
json_decode_string_cycle_start:
|
||||||
assert(*p == '{' || next_map_special == false);
|
assert(*p == '{' || next_map_special == false);
|
||||||
|
@ -451,6 +451,19 @@ describe('json_decode() function', function()
|
|||||||
it('parses U+00C3 correctly', function()
|
it('parses U+00C3 correctly', function()
|
||||||
eq('\xC3\x83', funcs.json_decode('"\xC3\x83"'))
|
eq('\xC3\x83', funcs.json_decode('"\xC3\x83"'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('fails to parse empty string', function()
|
||||||
|
eq('Vim(call):E474: Attempt to decode a blank string',
|
||||||
|
exc_exec('call json_decode("")'))
|
||||||
|
eq('Vim(call):E474: Attempt to decode a blank string',
|
||||||
|
exc_exec('call json_decode(" ")'))
|
||||||
|
eq('Vim(call):E474: Attempt to decode a blank string',
|
||||||
|
exc_exec('call json_decode("\\t")'))
|
||||||
|
eq('Vim(call):E474: Attempt to decode a blank string',
|
||||||
|
exc_exec('call json_decode("\\n")'))
|
||||||
|
eq('Vim(call):E474: Attempt to decode a blank string',
|
||||||
|
exc_exec('call json_decode(" \\t\\n \\n\\t\\t \\n\\t\\n \\n \\t\\n\\t ")'))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('json_encode() function', function()
|
describe('json_encode() function', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user