lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


My colleague found a bug introduced by
https://github.com/lua/lua/commit/5aa36e894f5a0348dfd19bd9cdcdd27ce8aa5f05
(Oct 6 15:50:24 No more field 'lua_State.stacksize' ) .

The test case :

local t = {}
setmetatable(t, t)
function t.__add(a, b)
    t[a] = b
    return t[a]
end

local a = 1
local b = 2
local c = 3
local d = 4
local e = 5
local a = 1
local b = 2
local c = 3
local d = 4
local e = 5
local a = 1
local b = 2
local c = 3
local d = 4
local e = 5
local a = 1
local b = 2
local b = 3
local d = 4
local e = 5
local a = 1
local b = 2
local b = 3
local a = 1
local b = 2
local b = 3
local b = 3
local c= t+2
print(c) -- nil,expect 2

This patch can fix it :

--- a/ldo.c
+++ b/ldo.c
@@ -191,7 +191,7 @@ int luaD_reallocstack (lua_State *L, int newsize,
int raiseerror) {
       luaM_error(L);
     else return 0;  /* do not raise an error */
   }
-  for (; lim < newsize; lim++)
+  for (; lim < newsize + EXTRA_STACK; lim++)
     setnilvalue(s2v(newstack + lim)); /* erase new segment */
   correctstack(L, L->stack, newstack);
   L->stack = newstack;
diff --git a/lstate.h b/lstate.h
index cbcf07e2..5f651108 100644
--- a/lstate.h
+++ b/lstate.h
@@ -139,7 +139,7 @@ struct lua_longjmp;  /* defined in ldo.c */

 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)

-#define stacksize(th)  cast_int((th)->stack_last - (th)->stack)
+#define stacksize(th)  cast_int((th)->stack_last + EXTRA_STACK - (th)->stack)

-- 
http://blog.codingnow.com