Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lua_tabletype function to improve serialization #224

Merged
merged 1 commit into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

cmake_minimum_required(VERSION 3.6 FATAL_ERROR)
project(luasandbox VERSION 1.2.11 LANGUAGES C)
project(luasandbox VERSION 1.3.0 LANGUAGES C)

set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Generic Lua sandbox for dynamic data analysis")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
Expand Down
8 changes: 8 additions & 0 deletions include/luasandbox/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8

/*
** table types
*/
#define LUA_TTEMPTY 0
#define LUA_TTARRAY 1
#define LUA_TTHASH 2
#define LUA_TTMIXED 3


/* minimum Lua stack available to a C function */
Expand Down Expand Up @@ -132,6 +139,7 @@ LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
** access functions (stack -> C)
*/

LUA_API int (lua_tabletype) (lua_State *L, int idx);
LUA_API int (lua_isnumber) (lua_State *L, int idx);
LUA_API int (lua_isstring) (lua_State *L, int idx);
LUA_API int (lua_iscfunction) (lua_State *L, int idx);
Expand Down
12 changes: 12 additions & 0 deletions src/lua/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ LUA_API int lua_iscfunction (lua_State *L, int idx) {
}


LUA_API int lua_tabletype (lua_State *L, int idx) {
StkId t;
int tt;
lua_lock(L);
t = index2adr(L, idx);
api_check(L, ttistable(t));
tt = luaH_type(hvalue(t));
lua_unlock(L);
return tt;
}


LUA_API int lua_isnumber (lua_State *L, int idx) {
TValue n;
const TValue *o = index2adr(L, idx);
Expand Down
7 changes: 7 additions & 0 deletions src/lua/ltable.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ int luaH_getn (Table *t) {
}


int luaH_type (Table *t) {
if (t->sizearray == 0) {
return t->node == dummynode ? LUA_TTEMPTY : LUA_TTHASH;
}
return t->node == dummynode ? LUA_TTARRAY : LUA_TTMIXED;
}


#if defined(LUA_DEBUG)

Expand Down
1 change: 1 addition & 0 deletions src/lua/ltable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
LUAI_FUNC int luaH_getn (Table *t);
LUAI_FUNC int luaH_type (Table *t);


#if defined(LUA_DEBUG)
Expand Down
9 changes: 7 additions & 2 deletions src/luasandbox_serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,13 @@ serialize_kvp(lsb_lua_sandbox *lsb, serialization_data *data, size_t parent)
seen = add_table_ref(&data->tables, ptr, pos);
if (seen != NULL) {
data->keys.pos += 1;
fprintf(data->fh, "%s = {}\n", data->keys.buf + pos);
ret = serialize_table(lsb, data, pos);
if (lua_tabletype(lsb->lua, vindex) == LUA_TTARRAY
&& lua_objlen(lsb->lua, vindex) == 0) {
fprintf(data->fh, "%s = {nil}\n", data->keys.buf + pos);
} else {
fprintf(data->fh, "%s = {}\n", data->keys.buf + pos);
ret = serialize_table(lsb, data, pos);
}
} else {
snprintf(lsb->error_message, LSB_ERROR_SIZE,
"lsb_serialize preserve table out of memory");
Expand Down
7 changes: 7 additions & 0 deletions src/test/lua/serialize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ data = ud.new("ud2")

dataRef = data

empty_array = {nil}
empty_array1 = {}
empty_array1[1] = nil
empty_object = {}
empty_object1 = {[1] = nil}
array = {1, "two", 3, "four", 5}

function process_message ()
return 0
end
Expand Down
12 changes: 11 additions & 1 deletion src/test/output/serialize.lua51.data
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
if _PRESERVATION_VERSION and _PRESERVATION_VERSION ~= 0 then return end
_G["ninf"] = -1/0
_G["empty_object1"] = {}
if _G["dataRef"] == nil then _G["dataRef"] = ud.new('ud2') end
_G["array"] = {}
_G["array"][1] = 1
_G["array"][2] = "two"
_G["array"][3] = 3
_G["array"][4] = "four"
_G["array"][5] = 5
_G["empty_object"] = {}
_G["rate"] = 0.12345678
_G["kvp"] = {}
_G["kvp"]["a"] = "foo"
Expand All @@ -12,6 +20,7 @@ _G["kvp"]["r"][4] = 92.002
_G["kvp"]["r"][5] = 91.10001
_G["kvp"]["r"]["key"] = "val"
_G["kvp"]["b"] = "bar"
_G["key with spaces"] = "kws"
_G["uuids"] = {}
_G["uuids"][1] = {}
_G["uuids"][1]["type"] = "test"
Expand All @@ -20,6 +29,7 @@ _G["uuids"][2] = {}
_G["uuids"][2]["type"] = "test1"
_G["uuids"][2]["uuid"] = "BD48B609-8922-4E59-A358-C242075CE089"
_G["inf"] = 1/0
_G["empty_array"] = {nil}
_G["data"] = _G["dataRef"]
_G["cycleb"] = {}
_G["cycleb"]["a"] = {}
Expand Down Expand Up @@ -48,7 +58,7 @@ _G["large_key"]["aaaaaaaaaaaaaaaaaaa"]["bbbbbbbbbbbbbbbbbbb"]["BD48B609-8922-4E5
_G["large_key"]["aaaaaaaaaaaaaaaaaaa"]["BD48B609-8922-4E59-A358-C242075CE081"] = 1
_G["count"] = 0
_G["boolean"] = true
_G["key with spaces"] = "kws"
_G["empty_array1"] = {nil}
_G["nested"] = {}
_G["nested"]["arg2"] = 2
_G["nested"]["nested"] = {}
Expand Down