aboutsummaryrefslogtreecommitdiff
path: root/loslib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-09-23 10:57:35 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-09-23 10:57:35 -0300
commitcfbe378f906061ee56f91acfbdf569d0d3fb9556 (patch)
tree26bde27fbb3ed4c158de4a9991a144dd3f2cd57b /loslib.c
parenta1089b415a3f5c753aa1b40758ffdaf28d5701b0 (diff)
downloadlua-cfbe378f906061ee56f91acfbdf569d0d3fb9556.tar.gz
lua-cfbe378f906061ee56f91acfbdf569d0d3fb9556.tar.bz2
lua-cfbe378f906061ee56f91acfbdf569d0d3fb9556.zip
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 (limited to 'loslib.c')
-rw-r--r--loslib.c4
1 files changed, 1 insertions, 3 deletions
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) {
260 res = d; 260 res = d;
261 } 261 }
262 else { 262 else {
263 /* unsigned avoids overflow when lua_Integer has 32 bits */ 263 if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res))
264 if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta
265 : (lua_Integer)INT_MIN + delta <= res))
266 return luaL_error(L, "field '%s' is out-of-bound", key); 264 return luaL_error(L, "field '%s' is out-of-bound", key);
267 res -= delta; 265 res -= delta;
268 } 266 }