diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-08 20:57:05 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-08 20:57:05 +0930 |
commit | 75ee6bfcf40b531024fad9a5ebb0908859769816 (patch) | |
tree | a9344b75a770bd5d439a7d505525a0f77d5cece3 | |
parent | 4dc56c6d362f2cd8a79d83369f0b852df07dae3f (diff) | |
download | lua-cjson-75ee6bfcf40b531024fad9a5ebb0908859769816.tar.gz lua-cjson-75ee6bfcf40b531024fad9a5ebb0908859769816.tar.bz2 lua-cjson-75ee6bfcf40b531024fad9a5ebb0908859769816.zip |
Generate error when attempting to decode UTF-16/32
-rw-r--r-- | lua_cjson.c | 8 | ||||
-rwxr-xr-x | tests/test.lua | 4 |
2 files changed, 12 insertions, 0 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index 52b259d..179c830 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -1167,6 +1167,14 @@ static int json_decode(lua_State *l) | |||
1167 | 1167 | ||
1168 | json = luaL_checklstring(l, 1, &len); | 1168 | json = luaL_checklstring(l, 1, &len); |
1169 | 1169 | ||
1170 | /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3) | ||
1171 | * | ||
1172 | * CJSON can support any simple data type, hence only the first | ||
1173 | * character is guaranteed to be ASCII (at worst: "). This is | ||
1174 | * still enough to detect whether the wrong encoding is in use. */ | ||
1175 | if (len >= 2 && (!json[0] || !json[1])) | ||
1176 | luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); | ||
1177 | |||
1170 | lua_json_decode(l, json, len); | 1178 | lua_json_decode(l, json, len); |
1171 | 1179 | ||
1172 | return 1; | 1180 | return 1; |
diff --git a/tests/test.lua b/tests/test.lua index 0e0aad8..ab2fe7c 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -64,6 +64,10 @@ local encode_table_tests = { | |||
64 | } | 64 | } |
65 | 65 | ||
66 | local decode_error_tests = { | 66 | local decode_error_tests = { |
67 | { json.decode, { '\0"\0"' }, | ||
68 | false, { "JSON parser does not support UTF-16 or UTF-32" } }, | ||
69 | { json.decode, { '"\0"\0' }, | ||
70 | false, { "JSON parser does not support UTF-16 or UTF-32" } }, | ||
67 | { json.decode, { '{ "unexpected eof": ' }, | 71 | { json.decode, { '{ "unexpected eof": ' }, |
68 | false, { "Expected value but found T_END at character 21" } }, | 72 | false, { "Expected value but found T_END at character 21" } }, |
69 | { json.decode, { '{ "extra data": true }, false' }, | 73 | { json.decode, { '{ "extra data": true }, false' }, |