aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-01-18 00:55:33 +1030
committerMark Pulford <mark@kyne.com.au>2012-03-04 18:54:35 +1030
commit0c6f2e488e17528ae42481d09879fd36551000e9 (patch)
treeaa12aab7e8619df8235eccb2c46368d822d45c33
parent8faf8490e518315a8eff17a76b019debe48104b4 (diff)
downloadlua-cjson-0c6f2e488e17528ae42481d09879fd36551000e9.tar.gz
lua-cjson-0c6f2e488e17528ae42481d09879fd36551000e9.tar.bz2
lua-cjson-0c6f2e488e17528ae42481d09879fd36551000e9.zip
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.
Diffstat (limited to '')
-rw-r--r--lua_cjson.c5
-rwxr-xr-xtests/test.lua7
2 files changed, 8 insertions, 4 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index 4a2ce0f..b64ed62 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -1034,7 +1034,7 @@ static void json_next_number_token(json_parse_t *json, json_token_t *token)
1034 */ 1034 */
1035static void json_next_token(json_parse_t *json, json_token_t *token) 1035static void json_next_token(json_parse_t *json, json_token_t *token)
1036{ 1036{
1037 json_token_type_t *ch2token = json->cfg->ch2token; 1037 const json_token_type_t *ch2token = json->cfg->ch2token;
1038 int ch; 1038 int ch;
1039 1039
1040 /* Eat whitespace. */ 1040 /* Eat whitespace. */
@@ -1149,7 +1149,8 @@ static void json_decode_descend(lua_State *l, json_parse_t *json, int slots)
1149 } 1149 }
1150 1150
1151 strbuf_free(json->tmp); 1151 strbuf_free(json->tmp);
1152 luaL_error(l, "Too many nested data structures"); 1152 luaL_error(l, "Found too many nested data structures (%d) at character %d",
1153 json->current_depth, json->ptr - json->data);
1153} 1154}
1154 1155
1155static void json_parse_object_context(lua_State *l, json_parse_t *json) 1156static void json_parse_object_context(lua_State *l, json_parse_t *json)
diff --git a/tests/test.lua b/tests/test.lua
index 4c00453..8bb5b95 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -162,15 +162,18 @@ local cjson_tests = {
162 true, { {{{{{ "nested" }}}}} } }, 162 true, { {{{{{ "nested" }}}}} } },
163 { "Decode array over nested limit [throw error]", 163 { "Decode array over nested limit [throw error]",
164 json.decode, { '[[[[[[ "nested" ]]]]]]' }, 164 json.decode, { '[[[[[[ "nested" ]]]]]]' },
165 false, { "Too many nested data structures" } }, 165 false, { "Found too many nested data structures (6) at character 6" } },
166 { "Decode object at nested limit", 166 { "Decode object at nested limit",
167 json.decode, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' }, 167 json.decode, { '{"a":{"b":{"c":{"d":{"e":"nested"}}}}}' },
168 true, { {a={b={c={d={e="nested"}}}}} } }, 168 true, { {a={b={c={d={e="nested"}}}}} } },
169 { "Decode object over nested limit [throw error]", 169 { "Decode object over nested limit [throw error]",
170 json.decode, { '{"a":{"b":{"c":{"d":{"e":{"f":"nested"}}}}}}' }, 170 json.decode, { '{"a":{"b":{"c":{"d":{"e":{"f":"nested"}}}}}}' },
171 false, { "Too many nested data structures" } }, 171 false, { "Found too many nested data structures (6) at character 26" } },
172 { "Set decode_max_depth(1000)", 172 { "Set decode_max_depth(1000)",
173 json.decode_max_depth, { 1000 }, true, { 1000 } }, 173 json.decode_max_depth, { 1000 }, true, { 1000 } },
174 { "Decode deeply nested array [throw error]",
175 json.decode, { string.rep("[", 1100) .. '1100' .. string.rep("]", 1100)},
176 false, { "Found too many nested data structures (1001) at character 1001" } },
174 177
175 -- Test encoding nested tables 178 -- Test encoding nested tables
176 { "Set encode_max_depth(5)", 179 { "Set encode_max_depth(5)",