lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 1a741157cb43021a90189a2c7019f9323aa6f101
parent 0c78de0d6df13269458d38199f0a8bbe7ce733f2
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu,  8 Oct 2015 12:55:09 -0300

avoid (undefined behavior) integer 'overflow' in left shift

Diffstat:
Mlbitlib.c | 9++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/lbitlib.c b/lbitlib.c @@ -1,5 +1,5 @@ /* -** $Id: lbitlib.c,v 1.27 2014/10/01 11:54:56 roberto Exp roberto $ +** $Id: lbitlib.c,v 1.28 2014/11/02 19:19:04 roberto Exp roberto $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ @@ -186,11 +186,10 @@ static int b_extract (lua_State *L) { static int b_replace (lua_State *L) { int w; lua_Unsigned r = trim(luaL_checkunsigned(L, 1)); - lua_Unsigned v = luaL_checkunsigned(L, 2); + lua_Unsigned v = trim(luaL_checkunsigned(L, 2)); int f = fieldargs(L, 3, &w); - int m = mask(w); - v &= m; /* erase bits outside given width */ - r = (r & ~(m << f)) | (v << f); + lua_Unsigned m = mask(w); + r = (r & ~(m << f)) | ((v & m) << f); lua_pushunsigned(L, r); return 1; }