Skip to content

Commit affd4e4

Browse files
committed
fix: stack overflow in table.concat function
The previous implementation of tableConcat was pushing all values and separators onto the Lua stack before concatenating them, which caused stack overflow with large tables. This fix changes the implementation to build the result string incrementally rather than pushing all values onto the stack first, preventing stack overflow with large tables. Example that would previously fail but now works correctly: ``` package main import "github.com/yuin/gopher-lua" func main() { var L *lua.LState var err error L = lua.NewState() defer L.Close() err = L.DoString(` local t = {} for i = 1, 10000 do t[i] = tostring(i) end local s = table.concat(t, ',') `) if err != nil { println(err.Error()) } } ```
1 parent ccacf66 commit affd4e4

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

tablelib.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,18 @@ func tableConcat(L *LState) int {
6666
L.Push(emptyLString)
6767
return 1
6868
}
69-
//TODO should flushing?
70-
retbottom := L.GetTop()
69+
result := ""
7170
for ; i <= j; i++ {
7271
v := tbl.RawGetInt(i)
7372
if !LVCanConvToString(v) {
7473
L.RaiseError("invalid value (%s) at index %d in table for concat", v.Type().String(), i)
7574
}
76-
L.Push(v)
75+
result += LVAsString(v)
7776
if i != j {
78-
L.Push(sep)
77+
result += LVAsString(sep)
7978
}
8079
}
81-
L.Push(stringConcat(L, L.GetTop()-retbottom, L.reg.Top()-1))
80+
L.Push(LString(result))
8281
return 1
8382
}
8483

0 commit comments

Comments
 (0)