Browse Source

extra trim applied to 'replace' argument (in case lua_Unsigned is
larger than 32 bits)

Roberto Ierusalimschy 12 years ago
parent
commit
44358e0206
1 changed files with 8 additions and 6 deletions
  1. 8 6
      lbitlib.c

+ 8 - 6
lbitlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbitlib.c,v 1.17 2013/02/21 13:44:53 roberto Exp roberto $
+** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp roberto $
 ** Standard library for bitwise operations
 ** See Copyright Notice in lua.h
 */
@@ -19,7 +19,12 @@
 #endif
 
 
-#define ALLONES		(~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
+/* type with (at least) LUA_NBITS bits */
+typedef unsigned long b_uint;
+
+
+#define ALLONES		(~(((~(b_uint)0) << (LUA_NBITS - 1)) << 1))
+
 
 /* macro to trim extra bits */
 #define trim(x)		((x) & ALLONES)
@@ -29,9 +34,6 @@
 #define mask(n)		(~((ALLONES << 1) << ((n) - 1)))
 
 
-typedef lua_Unsigned b_uint;
-
-
 
 static b_uint andaux (lua_State *L) {
   int i, n = lua_gettop(L);
@@ -165,7 +167,7 @@ static int fieldargs (lua_State *L, int farg, int *width) {
 
 static int b_extract (lua_State *L) {
   int w;
-  b_uint r = luaL_checkunsigned(L, 1);
+  b_uint r = trim(luaL_checkunsigned(L, 1));
   int f = fieldargs(L, 2, &w);
   r = (r >> f) & mask(w);
   lua_pushunsigned(L, r);