diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-08 23:34:17 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-08 23:34:17 +0930 |
commit | 1798471fcdd3425e7d2bafbd69048ff1165eb31e (patch) | |
tree | c3a27ebb41022bc2b765ebd6474ea56f8efb79b7 | |
parent | f59b3b671984d6a3bf7f7dd08512a89dd916a901 (diff) | |
download | lua-cjson-1798471fcdd3425e7d2bafbd69048ff1165eb31e.tar.gz lua-cjson-1798471fcdd3425e7d2bafbd69048ff1165eb31e.tar.bz2 lua-cjson-1798471fcdd3425e7d2bafbd69048ff1165eb31e.zip |
Add test for excessive nesting during encode
-rw-r--r-- | tests/common.lua | 17 | ||||
-rwxr-xr-x | tests/test.lua | 5 |
2 files changed, 16 insertions, 6 deletions
diff --git a/tests/common.lua b/tests/common.lua index b8ce01d..63d37c0 100644 --- a/tests/common.lua +++ b/tests/common.lua | |||
@@ -29,7 +29,7 @@ function is_array(table) | |||
29 | return max | 29 | return max |
30 | end | 30 | end |
31 | 31 | ||
32 | function serialise_table(value, indent) | 32 | function serialise_table(value, indent, depth) |
33 | local spacing, spacing2, indent2 | 33 | local spacing, spacing2, indent2 |
34 | if indent then | 34 | if indent then |
35 | spacing = "\n" .. indent | 35 | spacing = "\n" .. indent |
@@ -38,6 +38,10 @@ function serialise_table(value, indent) | |||
38 | else | 38 | else |
39 | spacing, spacing2, indent2 = " ", " ", false | 39 | spacing, spacing2, indent2 = " ", " ", false |
40 | end | 40 | end |
41 | depth = depth + 1 | ||
42 | if depth > 50 then | ||
43 | return "ERROR: Too many nested tables" | ||
44 | end | ||
41 | 45 | ||
42 | local max = is_array(value) | 46 | local max = is_array(value) |
43 | 47 | ||
@@ -49,7 +53,7 @@ function serialise_table(value, indent) | |||
49 | if comma then | 53 | if comma then |
50 | table.insert(fragment, "," .. spacing2) | 54 | table.insert(fragment, "," .. spacing2) |
51 | end | 55 | end |
52 | table.insert(fragment, serialise_value(value[i], indent2)) | 56 | table.insert(fragment, serialise_value(value[i], indent2, depth)) |
53 | comma = true | 57 | comma = true |
54 | end | 58 | end |
55 | elseif max < 0 then | 59 | elseif max < 0 then |
@@ -59,8 +63,8 @@ function serialise_table(value, indent) | |||
59 | table.insert(fragment, "," .. spacing2) | 63 | table.insert(fragment, "," .. spacing2) |
60 | end | 64 | end |
61 | table.insert(fragment, string.format( | 65 | table.insert(fragment, string.format( |
62 | "[%s] = %s", serialise_value(k, indent2), | 66 | "[%s] = %s", serialise_value(k, indent2, depth), |
63 | serialise_value(v, indent2)) | 67 | serialise_value(v, indent2, depth)) |
64 | ) | 68 | ) |
65 | comma = true | 69 | comma = true |
66 | end | 70 | end |
@@ -70,8 +74,9 @@ function serialise_table(value, indent) | |||
70 | return table.concat(fragment) | 74 | return table.concat(fragment) |
71 | end | 75 | end |
72 | 76 | ||
73 | function serialise_value(value, indent) | 77 | function serialise_value(value, indent, depth) |
74 | if indent == nil then indent = "" end | 78 | if indent == nil then indent = "" end |
79 | if depth == nil then depth = 0 end | ||
75 | 80 | ||
76 | if value == cjson.null then | 81 | if value == cjson.null then |
77 | return "cjson.null" | 82 | return "cjson.null" |
@@ -81,7 +86,7 @@ function serialise_value(value, indent) | |||
81 | type(value) == "boolean" then | 86 | type(value) == "boolean" then |
82 | return tostring(value) | 87 | return tostring(value) |
83 | elseif type(value) == "table" then | 88 | elseif type(value) == "table" then |
84 | return serialise_table(value, indent) | 89 | return serialise_table(value, indent, depth) |
85 | else | 90 | else |
86 | return "\"<" .. type(value) .. ">\"" | 91 | return "\"<" .. type(value) .. ">\"" |
87 | end | 92 | end |
diff --git a/tests/test.lua b/tests/test.lua index 436b20c..10d8989 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -63,6 +63,9 @@ if not utf8_loaded then | |||
63 | end | 63 | end |
64 | local utf16_escaped = gen_utf16_escaped() | 64 | local utf16_escaped = gen_utf16_escaped() |
65 | local nested5 = {{{{{ "nested" }}}}} | 65 | local nested5 = {{{{{ "nested" }}}}} |
66 | local table_cycle = {} | ||
67 | local table_cycle2 = { table_cycle } | ||
68 | table_cycle[1] = table_cycle2 | ||
66 | 69 | ||
67 | local decode_simple_tests = { | 70 | local decode_simple_tests = { |
68 | { json.decode, { '"test string"' }, true, { "test string" } }, | 71 | { json.decode, { '"test string"' }, true, { "test string" } }, |
@@ -124,6 +127,8 @@ local encode_table_tests = { | |||
124 | 127 | ||
125 | { json.encode, { nested5 }, true, { '[ [ [ [ [ "nested" ] ] ] ] ]' } }, | 128 | { json.encode, { nested5 }, true, { '[ [ [ [ [ "nested" ] ] ] ] ]' } }, |
126 | { json.encode, { { nested5 } }, | 129 | { json.encode, { { nested5 } }, |
130 | false, { "Cannot serialise, excessive nesting (6)" } }, | ||
131 | { json.encode, { table_cycle }, | ||
127 | false, { "Cannot serialise, excessive nesting (6)" } } | 132 | false, { "Cannot serialise, excessive nesting (6)" } } |
128 | } | 133 | } |
129 | 134 | ||