Skip to content

Commit c880cdb

Browse files
committed
Detect invalid argument indexing. Compared behavior with Lua 5.1 and LuaJIT.
1 parent f4c35e4 commit c880cdb

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

_glua-tests/issues.lua

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,33 @@ function test()
330330
t6.sec = "5"
331331
assert(os.time(t1) == os.time(t6))
332332
end
333-
test()
333+
test()
334+
335+
--issue #331
336+
function test()
337+
local select_a = function()
338+
return select(3, "1")
339+
end
340+
assert(true == pcall(select_a))
341+
local select_b = function()
342+
return select(0)
343+
end
344+
assert(false == pcall(select_b))
345+
local select_c = function()
346+
return select(1/9)
347+
end
348+
assert(false == pcall(select_c))
349+
local select_d = function()
350+
return select(1, "a")
351+
end
352+
assert("a" == select_d())
353+
local select_e = function()
354+
return select(3, "a", "b", "c")
355+
end
356+
assert("c" == select_e())
357+
local select_f = function()
358+
return select(0)(select(1/9))
359+
end
360+
assert(false == pcall(select_f))
361+
end
362+
test()

baselib.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,16 @@ func baseSelect(L *LState) int {
321321
switch lv := L.Get(1).(type) {
322322
case LNumber:
323323
idx := int(lv)
324-
num := L.reg.Top() - L.indexToReg(int(lv)) - 1
324+
num := L.GetTop()
325325
if idx < 0 {
326-
num++
326+
idx = num + idx
327+
} else if idx > num {
328+
idx = num
327329
}
328-
return num
330+
if 1 > idx {
331+
L.ArgError(1, "index out of range")
332+
}
333+
return num - idx
329334
case LString:
330335
if string(lv) != "#" {
331336
L.ArgError(1, "invalid string '"+string(lv)+"'")

0 commit comments

Comments
 (0)