diff options
-rw-r--r-- | lua_cjson.c | 19 | ||||
-rwxr-xr-x | tests/test.lua | 9 |
2 files changed, 21 insertions, 7 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 00fa2dd..4a2ce0f 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -564,12 +564,17 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) | |||
564 | static void json_check_encode_depth(lua_State *l, json_config_t *cfg, | 564 | static void json_check_encode_depth(lua_State *l, json_config_t *cfg, |
565 | int current_depth, strbuf_t *json) | 565 | int current_depth, strbuf_t *json) |
566 | { | 566 | { |
567 | if (current_depth > cfg->encode_max_depth) { | 567 | /* Ensure there are enough slots free to traverse a table (key, value). |
568 | if (!cfg->encode_keep_buffer) | 568 | * luaL_error() and other Lua API functions use space from the |
569 | strbuf_free(json); | 569 | * "EXTRA_STACK" reserve. */ |
570 | luaL_error(l, "Cannot serialise, excessive nesting (%d)", | 570 | if (current_depth <= cfg->encode_max_depth && lua_checkstack(l, 2)) |
571 | current_depth); | 571 | return; |
572 | } | 572 | |
573 | if (!cfg->encode_keep_buffer) | ||
574 | strbuf_free(json); | ||
575 | |||
576 | luaL_error(l, "Cannot serialise, excessive nesting (%d)", | ||
577 | current_depth); | ||
573 | } | 578 | } |
574 | 579 | ||
575 | static void json_append_data(lua_State *l, json_config_t *cfg, | 580 | static void json_append_data(lua_State *l, json_config_t *cfg, |
@@ -692,9 +697,9 @@ static void json_append_data(lua_State *l, json_config_t *cfg, | |||
692 | strbuf_append_mem(json, "false", 5); | 697 | strbuf_append_mem(json, "false", 5); |
693 | break; | 698 | break; |
694 | case LUA_TTABLE: | 699 | case LUA_TTABLE: |
695 | len = lua_array_length(l, cfg, json); | ||
696 | current_depth++; | 700 | current_depth++; |
697 | json_check_encode_depth(l, cfg, current_depth, json); | 701 | json_check_encode_depth(l, cfg, current_depth, json); |
702 | len = lua_array_length(l, cfg, json); | ||
698 | if (len > 0) | 703 | if (len > 0) |
699 | json_append_array(l, cfg, current_depth, json, len); | 704 | json_append_array(l, cfg, current_depth, json, len); |
700 | else | 705 | else |
diff --git a/tests/test.lua b/tests/test.lua index a827e6a..4c00453 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -67,6 +67,12 @@ function load_testdata() | |||
67 | data.table_cycle = {} | 67 | data.table_cycle = {} |
68 | data.table_cycle[1] = data.table_cycle | 68 | data.table_cycle[1] = data.table_cycle |
69 | 69 | ||
70 | local big = {} | ||
71 | for i = 1, 1100 do | ||
72 | big = { { 10, false, true, cjson.null }, "string", a = big } | ||
73 | end | ||
74 | data.deeply_nested_data = big | ||
75 | |||
70 | return data | 76 | return data |
71 | end | 77 | end |
72 | 78 | ||
@@ -185,6 +191,9 @@ local cjson_tests = { | |||
185 | false, { "Cannot serialise, excessive nesting (6)" } }, | 191 | false, { "Cannot serialise, excessive nesting (6)" } }, |
186 | { "Set encode_max_depth(1000)", | 192 | { "Set encode_max_depth(1000)", |
187 | json.encode_max_depth, { 1000 }, true, { 1000 } }, | 193 | json.encode_max_depth, { 1000 }, true, { 1000 } }, |
194 | { "Encode deeply nested data [throw error]", | ||
195 | json.encode, { testdata.deeply_nested_data }, | ||
196 | false, { "Cannot serialise, excessive nesting (1001)" } }, | ||
188 | 197 | ||
189 | -- Test encoding simple types | 198 | -- Test encoding simple types |
190 | { "Encode null", | 199 | { "Encode null", |