diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-10-08 12:55:35 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-10-08 12:55:35 -0300 |
commit | 1a741157cb43021a90189a2c7019f9323aa6f101 (patch) | |
tree | 639f053f33fb71d06033cd6d1cdc7131a2043fc3 | |
parent | 0c78de0d6df13269458d38199f0a8bbe7ce733f2 (diff) | |
download | lua-1a741157cb43021a90189a2c7019f9323aa6f101.tar.gz lua-1a741157cb43021a90189a2c7019f9323aa6f101.tar.bz2 lua-1a741157cb43021a90189a2c7019f9323aa6f101.zip |
avoid (undefined behavior) integer 'overflow' in left shift
-rw-r--r-- | lbitlib.c | 9 |
1 files changed, 4 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbitlib.c,v 1.27 2014/10/01 11:54:56 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp roberto $ |
3 | ** Standard library for bitwise operations | 3 | ** Standard library for bitwise operations |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -186,11 +186,10 @@ static int b_extract (lua_State *L) { | |||
186 | static int b_replace (lua_State *L) { | 186 | static int b_replace (lua_State *L) { |
187 | int w; | 187 | int w; |
188 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); | 188 | lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); |
189 | lua_Unsigned v = luaL_checkunsigned(L, 2); | 189 | lua_Unsigned v = trim(luaL_checkunsigned(L, 2)); |
190 | int f = fieldargs(L, 3, &w); | 190 | int f = fieldargs(L, 3, &w); |
191 | int m = mask(w); | 191 | lua_Unsigned m = mask(w); |
192 | v &= m; /* erase bits outside given width */ | 192 | r = (r & ~(m << f)) | ((v & m) << f); |
193 | r = (r & ~(m << f)) | (v << f); | ||
194 | lua_pushunsigned(L, r); | 193 | lua_pushunsigned(L, r); |
195 | return 1; | 194 | return 1; |
196 | } | 195 | } |