Skip to content

Commit c393c9b

Browse files
committed
Added some assertions to prevent use of bitwise operations on numbers without integer precision. A refactoring will be necessary to make sure these simple bitwise operations are used on 32 bit integers, which right now is not the case.
1 parent f7e77f7 commit c393c9b

1 file changed

Lines changed: 18 additions & 32 deletions

File tree

wirebait.lua

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,52 +76,38 @@ end
7676
local UINT32_MAX = 0xFFFFFFFF;-- 32 bit word
7777
local WORD_MASK = UINT32_MAX;
7878
local function bwAnd(int1, int2) --TODO: enforce uint32 params!
79-
if lua_version_int < 53 then
80-
return bit32.band(int1, int2);
81-
else
82-
return int1.__band(int2);
83-
end
79+
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
80+
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
81+
return bit32.band(int1, int2);
8482
end
8583

8684
local function bwLshift(int1, int2)
87-
if lua_version_int < 53 then
88-
return int1 * math.pow(2,int2);
89-
--return bit32.lshift(int1, int2);
90-
else
91-
return int1:__shl(int2);
92-
end
85+
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
86+
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
87+
return int1 * math.pow(2,int2);
9388
end
9489

9590
local function bwRshift(int1, int2)
96-
if lua_version_int < 53 then
97-
return bit32.rshift(int1, int2);
98-
else
99-
return int1.__shr(int2);
100-
end
91+
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
92+
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
93+
return bit32.rshift(int1, int2);
10194
end
10295

10396
local function bwOr(int1, int2)
104-
if lua_version_int < 53 then
105-
return bit32.bor(int1, int2);
106-
else
107-
return int1.__bor(int2);
108-
end
97+
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
98+
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
99+
return bit32.bor(int1, int2);
109100
end
110101

111102
local function bwXor(int1, int2)
112-
if lua_version_int < 53 then
113-
return bit32.bxor(int1, int2);
114-
else
115-
return int1.__bxor(int2);
116-
end
103+
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
104+
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
105+
return bit32.bxor(int1, int2);
117106
end
118107

119-
local function bwNot(int1, int2)
120-
if lua_version_int < 53 then
121-
return bit32.bnot(int1, int2);
122-
else
123-
return int1.__bnot(int2);
124-
end
108+
local function bwNot(int1)
109+
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting unsigned");
110+
return bit32.bnot(int1);
125111
end
126112

127113

0 commit comments

Comments
 (0)