From 89f9e6a46fd5528d91b114ff65b92e185a54a0f5 Mon Sep 17 00:00:00 2001 From: Richard Hundt Date: Sat, 8 Sep 2012 11:12:41 +0200 Subject: [PATCH] FIX extra arguments breaking the stack --- lmarshal.c | 7 ++++++- test.lua | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lmarshal.c b/lmarshal.c index ee766f6..9ab8d23 100644 --- a/lmarshal.c +++ b/lmarshal.c @@ -457,15 +457,19 @@ static int mar_encode(lua_State* L) else if (!lua_istable(L, 2)) { luaL_error(L, "bad argument #2 to encode (expected table)"); } + lua_settop(L, 2); len = lua_objlen(L, 2); lua_newtable(L); for (idx = 1; idx <= len; idx++) { lua_rawgeti(L, 2, idx); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + continue; + } lua_pushinteger(L, idx); lua_rawset(L, SEEN_IDX); } - lua_pushvalue(L, 1); buf_init(L, &buf); @@ -500,6 +504,7 @@ static int mar_decode(lua_State* L) else if (!lua_istable(L, 2)) { luaL_error(L, "bad argument #2 to decode (expected table)"); } + lua_settop(L, 2); len = lua_objlen(L, 2); lua_newtable(L); diff --git a/test.lua b/test.lua index 86d68dd..26349bb 100644 --- a/test.lua +++ b/test.lua @@ -138,6 +138,11 @@ assert(f2() == 'answer: 42') assert(marshal.decode(marshal.encode()) == nil) assert(marshal.decode(marshal.encode(nil)) == nil) + +local s1 = marshal.encode(pt) +local p2 = marshal.decode(s1) +print(string.format('%q',s1)) + print "OK" --[[ micro-bench (~4.2 seconds on my laptop) @@ -149,3 +154,4 @@ for i=1, 1000000 do end --]] --]==] +