commit cfbe378f906061ee56f91acfbdf569d0d3fb9556
parent a1089b415a3f5c753aa1b40758ffdaf28d5701b0
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Fri, 23 Sep 2022 10:57:08 -0300
Small simplification in overflow check in 'getfield'
Subtracting a small non-negative int from a non-negative int cannot
overflow, and adding a non-negative int to INT_MIN cannot overflow.
Diffstat:
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/loslib.c b/loslib.c
@@ -260,9 +260,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
res = d;
}
else {
- /* unsigned avoids overflow when lua_Integer has 32 bits */
- if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta
- : (lua_Integer)INT_MIN + delta <= res))
+ if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
return luaL_error(L, "field '%s' is out-of-bound", key);
res -= delta;
}
diff --git a/testes/files.lua b/testes/files.lua
@@ -825,8 +825,17 @@ checkerr("missing", os.time, {hour = 12}) -- missing date
if string.packsize("i") == 4 then -- 4-byte ints
checkerr("field 'year' is out-of-bound", os.time,
{year = -(1 << 31) + 1899, month = 1, day = 1})
+
+ checkerr("field 'year' is out-of-bound", os.time,
+ {year = -(1 << 31), month = 1, day = 1})
+
+ if math.maxinteger > 2^31 then -- larger lua_integer?
+ checkerr("field 'year' is out-of-bound", os.time,
+ {year = (1 << 31) + 1900, month = 1, day = 1})
+ end
end
+
if not _port then
-- test Posix-specific modifiers
assert(type(os.date("%Ex")) == 'string')