From 44358e0206c3ec034a6a06e7ad4dc5e254874201 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 16 Apr 2013 15:39:37 -0300
Subject: extra trim applied to 'replace' argument (in case lua_Unsigned is
 larger than 32 bits)

---
 lbitlib.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/lbitlib.c b/lbitlib.c
index 8650dc34..faba3c6d 100644
--- a/lbitlib.c
+++ b/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);
-- 
cgit v1.2.3-55-g6feb