Bug in Lua 5.4.2 (github master)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug in Lua 5.4.2 (github master)

云风 Cloud Wu
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
Reply | Threaded
Open this post in threaded view
|

Re: Bug in Lua 5.4.2 (github master)

云风 Cloud Wu
云风 Cloud Wu <[hidden email]> 于2020年11月4日周三 上午10:10写道:

>
> 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)
>

Sorry , I should not change the macro stacksize. The patch should be :

--- a/src/ldo.c
+++ b/src/ldo.c
@@ -192,7 +192,7 @@ int luaD_reallocstack (lua_State *L, int newsize,
int raiseerror) {
     else return 0;  /* do not raise an error */
   }
   for (; lim < newsize; lim++)
-    setnilvalue(s2v(newstack + lim)); /* erase new segment */
+    setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */
   correctstack(L, L->stack, newstack);
   L->stack = newstack;
   L->stack_last = L->stack + newsize;


--
http://blog.codingnow.com
Reply | Threaded
Open this post in threaded view
|

Re: Bug in Lua 5.4.2 (github master)

云风 Cloud Wu
云风 Cloud Wu <[hidden email]> 于2020年11月4日周三 上午11:01写道:

>
> Sorry , I should not change the macro stacksize. The patch should be :
>
> --- a/src/ldo.c
> +++ b/src/ldo.c
> @@ -192,7 +192,7 @@ int luaD_reallocstack (lua_State *L, int newsize,
> int raiseerror) {
>      else return 0;  /* do not raise an error */
>    }
>    for (; lim < newsize; lim++)
> -    setnilvalue(s2v(newstack + lim)); /* erase new segment */
> +    setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */
>    correctstack(L, L->stack, newstack);
>    L->stack = newstack;
>    L->stack_last = L->stack + newsize;
>

I'm not sure if it is necessary to init the extra slots of the stack,
but it seems that keeping values initialized would be better .

--- a/lstate.c
+++ b/lstate.c
@@ -181,7 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) {
int i; CallInfo *ci;
/* initialize stack array */
L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
- for (i = 0; i < BASIC_STACK_SIZE; i++)
+ for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)



--
http://blog.codingnow.com
Reply | Threaded
Open this post in threaded view
|

Re: Bug in Lua 5.4.2 (github master)

Roberto Ierusalimschy
In reply to this post by 云风 Cloud Wu
> 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' ) .
>
> [...]

Many thanks for the report.

Indeed, when the stack grows, what was the extra area becomes part
of the common area, and it may or may not be in use. So, it should
be kept clean all the time.

-- Roberto