webmcp
changeset 166:7885d1ae35ff
Minor bugfixes in JSON library (including checking type of arguments)
author | jbe |
---|---|
date | Thu Jul 31 23:33:28 2014 +0200 (2014-07-31) |
parents | 0c230f701967 |
children | 84497222db4e |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Thu Jul 31 20:54:15 2014 +0200 1.2 +++ b/libraries/json/json.c Thu Jul 31 23:33:28 2014 +0200 1.3 @@ -55,6 +55,8 @@ 1.4 lua_newtable(L); 1.5 lua_rawset(L, -3); 1.6 } else { 1.7 + // require argument to be a table: 1.8 + luaL_checktype(L, 1, LUA_TTABLE); 1.9 // push shadow table on top of stack: 1.10 json_regfetch(L, shadowtbl); 1.11 lua_pushvalue(L, 1); 1.12 @@ -125,6 +127,10 @@ 1.13 char *cbuf; // C buffer to decode JSON string values 1.14 size_t outlen; // maximum length or write position of C buffer 1.15 size_t arraylen; // variable to temporarily store the array length 1.16 + // require string as argument and convert to C string with length information: 1.17 + str = luaL_checklstring(L, 1, &total); 1.18 + // if string contains a NULL byte, this is a syntax error 1.19 + if (strlen(str) != total) goto json_import_syntax_error; 1.20 // stack shall contain one function argument: 1.21 lua_settop(L, 1); 1.22 // push objectmt onto stack position 2: 1.23 @@ -133,10 +139,6 @@ 1.24 json_regfetch(L, arraymt); 1.25 // push shadowtbl onto stack position 4: 1.26 json_regfetch(L, shadowtbl); 1.27 - // require string as first argument: 1.28 - str = luaL_checklstring(L, 1, &total); 1.29 - // if string contains a NULL byte, this is a syntax error 1.30 - if (strlen(str) != total) goto json_import_syntax_error; 1.31 // main loop of parser: 1.32 json_import_loop: 1.33 // skip whitespace and store next character in variable 'c': 1.34 @@ -924,7 +926,7 @@ 1.35 json_export_tabletype_error: 1.36 return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)"); 1.37 } 1.38 - return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, 1))); 1.39 + return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_internal_value_idx))); 1.40 } 1.41 1.42 static int json_export(lua_State *L) {