Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add depth/index to decode depth error message

Include depth and character index when throwing decode nesting errors.

Pre-emptively add a test decoding a massively nested JSON array. Lua
stack overflow faults are unlikely to occur on simple data structures.
Valgrind can highlight stack allocation bugs with complicated JSON even
if the test succeeds.
  • Loading branch information...
commit 0c6f2e488e17528ae42481d09879fd36551000e9 1 parent 8faf849
@mpx authored
Showing with 8 additions and 4 deletions.
  1. +3 −2 lua_cjson.c
  2. +5 −2 tests/test.lua
View
5 lua_cjson.c
@@ -1034,7 +1034,7 @@ static void json_next_number_token(json_parse_t *json, json_token_t *token)
*/
static void json_next_token(json_parse_t *json, json_token_t *token)
{
- json_token_type_t *ch2token = json->cfg->ch2token;
+ const json_token_type_t *ch2token = json->cfg->ch2token;
int ch;
/* Eat whitespace. */
@@ -1149,7 +1149,8 @@ static void json_decode_descend(lua_State *l, json_parse_t *json, int slots)
}
strbuf_free(json->tmp);
- luaL_error(l, "Too many nested data structures");
+ luaL_error(l, "Found too many nested data structures (%d) at character %d",
+ json->current_depth, json->ptr - json->data);
}
static void json_parse_object_context(lua_State *l, json_parse_t *json)
View
7 tests/test.lua
@@ -162,15 +162,18 @@ local cjson_tests = {
true, { {{{{{ "nested" }}}}} } },
{ "Decode array over nested limit [throw error]",
json.decode, { '[[[[[[ "nested" ]]]]]]' },
- false, { "Too many nested data structures" } },
+ false, { "Found too many nested data structures (6) at character 6" } },
{ "Decode object at nested limit",
json.decode, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' },
true, { {a={b={c={d={e="nested"}}}}} } },
{ "Decode object over nested limit [throw error]",
json.decode, { '{"a":{"b":{"c":{"d":{"e":{"f":"nested"}}}}}}' },
- false, { "Too many nested data structures" } },
+ false, { "Found too many nested data structures (6) at character 26" } },
{ "Set decode_max_depth(1000)",
json.decode_max_depth, { 1000 }, true, { 1000 } },
+ { "Decode deeply nested array [throw error]",
+ json.decode, { string.rep("[", 1100) .. '1100' .. string.rep("]", 1100)},
+ false, { "Found too many nested data structures (1001) at character 1001" } },
-- Test encoding nested tables
{ "Set encode_max_depth(5)",
Please sign in to comment.
Something went wrong with that request. Please try again.