# HG changeset patch # User jbe # Date 1406842408 -7200 # Node ID 7885d1ae35ffbaf7adab6b14efdda0da8f4c0aa2 # Parent 0c230f70196724c295b22192d2d42a3a0fb13101 Minor bugfixes in JSON library (including checking type of arguments) diff -r 0c230f701967 -r 7885d1ae35ff libraries/json/json.c --- a/libraries/json/json.c Thu Jul 31 20:54:15 2014 +0200 +++ b/libraries/json/json.c Thu Jul 31 23:33:28 2014 +0200 @@ -55,6 +55,8 @@ lua_newtable(L); lua_rawset(L, -3); } else { + // require argument to be a table: + luaL_checktype(L, 1, LUA_TTABLE); // push shadow table on top of stack: json_regfetch(L, shadowtbl); lua_pushvalue(L, 1); @@ -125,6 +127,10 @@ char *cbuf; // C buffer to decode JSON string values size_t outlen; // maximum length or write position of C buffer size_t arraylen; // variable to temporarily store the array length + // require string as argument and convert to C string with length information: + str = luaL_checklstring(L, 1, &total); + // if string contains a NULL byte, this is a syntax error + if (strlen(str) != total) goto json_import_syntax_error; // stack shall contain one function argument: lua_settop(L, 1); // push objectmt onto stack position 2: @@ -133,10 +139,6 @@ json_regfetch(L, arraymt); // push shadowtbl onto stack position 4: json_regfetch(L, shadowtbl); - // require string as first argument: - str = luaL_checklstring(L, 1, &total); - // if string contains a NULL byte, this is a syntax error - if (strlen(str) != total) goto json_import_syntax_error; // main loop of parser: json_import_loop: // skip whitespace and store next character in variable 'c': @@ -924,7 +926,7 @@ json_export_tabletype_error: return luaL_error(L, "JSON export not possible for ambiguous table (cannot decide whether it is an object or array)"); } - return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, 1))); + return luaL_error(L, "JSON export not possible for values of type \"%s\"", lua_typename(L, lua_type(L, json_export_internal_value_idx))); } static int json_export(lua_State *L) {