From cfbe378f906061ee56f91acfbdf569d0d3fb9556 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Sep 2022 10:57:35 -0300 Subject: 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. --- loslib.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'loslib.c') diff --git a/loslib.c b/loslib.c index 3e20d622..854dcf69 100644 --- 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; } -- cgit v1.2.3-55-g6feb