Tested on Arch Linux (glibc 2.42, gcc 15.2.1 20251112, make 4.4.1).
Builds obtained from official download on website (/ftp/lua-X.Y.Z.tar.gz)
Following codeblock causes stack overflow on version 5.4.4 and further (up to current 5.5.0).
--------
local function f(n)
if n == 0 then return end
for _ in pairs({1}) do return f(n - 1) end
end
f(1e6)
--------
Produced error:
--------
lua: stackov.lua:3: stack overflow
stack traceback:
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
... (skipping 142833 levels)
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in upvalue 'f'
stackov.lua:3: in local 'f'
stackov.lua:6: in main chunk
[C]: in ?
--------
Notably: since 5.5.0 instead of 142833 skipped levels, 166642 is skipped
On versions prior to 5.4.4 (5.4.3 and earlier) it does not produce any errors.
Probably it has something to do which changes introduced in 5.4.4 about to-be-closed variables and their logic/handling inside of nested functions or for loops.
Maybe it is similar case to "C-stack overflow with deep nesting of coroutine.close." issue reported by Xmilia Hermit on 13 Oct 2022.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/15c6cf42-d992-4d14-8c05-8e43ff624688%40yandex.ru.
With 1 million recursive non-trailing function calls, it's quite normal you get a stack overflow, stacks are not infinite.
-- Shmuel
Tested in on Windows with Lua 5.1 through 5.4 and LuaJIT.
Lua 5.4 causes stack overflow when parameter 'n' is less than 200,000.
Other Lua / LuaJIT versions don't cause stack overflow even at n == 100,000,000.
1 param, 7 slots, 2 upvalues, 5 locals, 3 constants, 0 functions
1 [2] EQ 0 0 -1 ; - 0
2 [2] JMP 0 1 ; to 4
3 [2] RETURN 0 1
4 [3] GETTABUP 1 0 -2 ; _ENV "pairs"
5 [3] NEWTABLE 2 1 0
6 [3] LOADK 3 -3 ; 1
7 [3] SETLIST 2 1 1 ; 1
8 [3] CALL 1 2 4
9 [3] JMP 0 4 ; to 14
10 [3] GETUPVAL 5 1 ; f
11 [3] SUB 6 0 -3 ; - 1
12 [3] TAILCALL 5 2 0
13 [3] RETURN 5 0
14 [3] TFORCALL 1 1
15 [3] TFORLOOP 3 -6 ; to 10
16 [4] RETURN 0 1
1 param, 8 slots, 2 upvalues, 6 locals, 1 constant, 0 functions
1 [2] EQI 0 0 0
2 [2] JMP 1 ; to 4
3 [2] RETURN 1 1 0k ; 0 out
4 [3] GETTABUP 1 0 0 ; _ENV "pairs"
5 [3] NEWTABLE 2 0 1 ; 1
6 [3] EXTRAARG 0
7 [3] LOADI 3 1
8 [3] SETLIST 2 1 0
9 [3] CALL 1 2 5 ; 1 in 4 out
10 [3] TFORPREP 1 5 ; to 16
11 [3] GETUPVAL 6 1 ; f
12 [3] ADDI 7 0 -1
13 [3] MMBINI 0 1 7 0 ; __sub
14 [3] CALL 6 2 0 ; 1 in all out
15 [3] RETURN 6 0 0k ; all out
16 [3] TFORCALL 1 1
17 [3] TFORLOOP 1 7 ; to 11
18 [3] CLOSE 1
19 [4] RETURN 1 1 0k ; 0 out