LuaDist/lua — pull request #7 by jaredlwong (2013-12-05 05:30:35+00:00)
Title: Fixed a signed integer overflow check.

Signed integer overflow is undefined behavior in C. In this commit we patch a
signed integer check in ltablib.c in the function unpack.

This patch may need to be modified under the constraints of two operations

ltablib.c:140
i = luaL_optint(L, 2, 1)

ltablib.c:141
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)

Here, we assume i and e can be any value in its given type, int. If this is
true, we must check for some special cases. If i is INT_MIN, we can't just take
the negative value of it, as -INT_MIN = INT_MAX+1 (theoretically) which is
undefined behavior.

The original check against the value of

n = e - i + 1

was

n <= 0

to check for signed integer overflow. Signed integer overflow is undefined
behavior.