Skip to content

fix(tablelib): follow Lua 5.4 range rules in table.concat - #89

Merged
wolfy-j merged 1 commit into
mainfrom
fix/table-concat-range
Nov 15, 2026
Merged

wolfy-j merged 1 commit into
mainfrom
fix/table-concat-range

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Defect

table.concat clamps i and j to the table length before it concatenates:

local t = {1, 2, 3}
print(table.concat(t, ",", 4, 3))      -- "3", Lua 5.4: ""
print(table.concat(t, ",", 9, 5))      -- "3", Lua 5.4: ""
print(table.concat(t, ",", 1, 5))      -- "1,2,3", Lua 5.4: error, invalid value at index 4
print(table.concat({[0] = "z", "a"}, ",", 0, 1))  -- "a", Lua 5.4: "z,a"
print(math.type(table.concat({1, 2}, ",", 2, 2))) -- "integer", Lua 5.4: result is the string "2"

Cause

tableConcat clamps i to [1, #t] and j to #t, so an empty range whose i lies past the border becomes a one-element range, out-of-range elements are skipped instead of reported, and indices below 1 are dropped. It then concatenates through the VM concat helper, which returns a lone operand unconverted.

Fix

tableConcat follows tconcat from Lua 5.4: it walks i..j exactly as given, an empty range yields "", every element must be a string or a number (otherwise the existing "invalid value" error is raised with the index), and the result is built as a string.

Tests

tablelib_concat_test.go covers whole and partial ranges, i > j inside and past the border, empty tables, index 0 and negative indices, a single element (string result), and the errors for j past the border and i below 1. Expected values come from the Lua 5.4 reference interpreter. 7 cases fail on main.

Verification

go vet ./..., golangci-lint v2.8.0 with the CI flags, go test ./... (full fixture corpus), the coverage collector run, and the race "other" shard (go test -race on all non-fixture tests and the non-regression fixture categories) pass. The regression-fixture race shards are left to CI.

The runtime picks this up through a go-lua dependency bump after merge.

table.concat clamped i and j to the table length before concatenating, so
a range with i > j could still return an element (concat(t, ',', 4, 3)
returned t[3]), ranges past the border were silently truncated instead of
raising, and indices below 1 were ignored. A single element was returned
unconverted, so concat({1}) produced a number.

table.concat now walks i..j exactly as lua's tconcat does: an empty range
gives "", every element in the range must be a string or number, and the
result is always a string.
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 17:26
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 18:48
@wolfy-j
wolfy-j merged commit 81d0954 into main Nov 15, 2026
12 checks passed
@wolfy-j
wolfy-j deleted the fix/table-concat-range branch November 15, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants