diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-01 02:40:50 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-01 02:40:50 +0930 |
commit | af1fe38223e23b2bb90f65bc6dd1b7e0dd5cb5cd (patch) | |
tree | 13655636e988ec860b174dc8ffbeef02f9a42e23 | |
parent | 60fb31cfdd625ea3bc4a12a8440715c8ff0c9242 (diff) | |
download | lua-cjson-af1fe38223e23b2bb90f65bc6dd1b7e0dd5cb5cd.tar.gz lua-cjson-af1fe38223e23b2bb90f65bc6dd1b7e0dd5cb5cd.tar.bz2 lua-cjson-af1fe38223e23b2bb90f65bc6dd1b7e0dd5cb5cd.zip |
Encode very sparse arrays as objects
Detect and encode very sparse arrays as objects. This prevents something
like:
{ [1000000] = "nullfest" }
..from generating a huge array.
-rw-r--r-- | lua_cjson.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 203b3b8..b87aebf 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -22,7 +22,6 @@ | |||
22 | * - Option to encode non-printable characters? Only \" \\ are required | 22 | * - Option to encode non-printable characters? Only \" \\ are required |
23 | * - Protect against cycles when encoding JSON from a data structure | 23 | * - Protect against cycles when encoding JSON from a data structure |
24 | * - Max depth? Notice cycles? | 24 | * - Max depth? Notice cycles? |
25 | * - Handle huge sparse arrays? | ||
26 | */ | 25 | */ |
27 | 26 | ||
28 | #include <assert.h> | 27 | #include <assert.h> |
@@ -36,6 +35,11 @@ | |||
36 | 35 | ||
37 | #include "strbuf.h" | 36 | #include "strbuf.h" |
38 | 37 | ||
38 | /* Encode very sparse arrays as objects */ | ||
39 | #ifndef VERY_SPARSE_ARRAY_RATIO | ||
40 | #define VERY_SPARSE_ARRAY_RATIO 2 | ||
41 | #endif | ||
42 | |||
39 | /* ===== ENCODING ===== */ | 43 | /* ===== ENCODING ===== */ |
40 | 44 | ||
41 | static void json_encode_exception(lua_State *l, strbuf_t *json, | 45 | static void json_encode_exception(lua_State *l, strbuf_t *json, |
@@ -113,8 +117,10 @@ static int lua_array_length(lua_State *l) | |||
113 | { | 117 | { |
114 | double k; | 118 | double k; |
115 | int max; | 119 | int max; |
120 | int items; | ||
116 | 121 | ||
117 | max = 0; | 122 | max = 0; |
123 | items = 0; | ||
118 | 124 | ||
119 | lua_pushnil(l); | 125 | lua_pushnil(l); |
120 | /* table, startkey */ | 126 | /* table, startkey */ |
@@ -126,6 +132,7 @@ static int lua_array_length(lua_State *l) | |||
126 | if (floor(k) == k && k >= 1) { | 132 | if (floor(k) == k && k >= 1) { |
127 | if (k > max) | 133 | if (k > max) |
128 | max = k; | 134 | max = k; |
135 | items++; | ||
129 | lua_pop(l, 1); | 136 | lua_pop(l, 1); |
130 | continue; | 137 | continue; |
131 | } | 138 | } |
@@ -136,6 +143,12 @@ static int lua_array_length(lua_State *l) | |||
136 | return -1; | 143 | return -1; |
137 | } | 144 | } |
138 | 145 | ||
146 | #ifdef VERY_SPARSE_ARRAY_RATIO | ||
147 | /* Encode very sparse arrays as objects */ | ||
148 | if (max > items * VERY_SPARSE_ARRAY_RATIO) | ||
149 | return -1; | ||
150 | #endif | ||
151 | |||
139 | return max; | 152 | return max; |
140 | } | 153 | } |
141 | 154 | ||