Skip to content

Commit

Permalink
Added some assertions to prevent use of bitwise operations on numbers…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
MarkoPaul0 committed Apr 7, 2018
1 parent f7e77f7 commit c393c9b
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions wirebait.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,38 @@ end
local UINT32_MAX = 0xFFFFFFFF;-- 32 bit word
local WORD_MASK = UINT32_MAX;
local function bwAnd(int1, int2) --TODO: enforce uint32 params!
if lua_version_int < 53 then
return bit32.band(int1, int2);
else
return int1.__band(int2);
end
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
return bit32.band(int1, int2);
end

local function bwLshift(int1, int2)
if lua_version_int < 53 then
return int1 * math.pow(2,int2);
--return bit32.lshift(int1, int2);
else
return int1:__shl(int2);
end
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
return int1 * math.pow(2,int2);
end

local function bwRshift(int1, int2)
if lua_version_int < 53 then
return bit32.rshift(int1, int2);
else
return int1.__shr(int2);
end
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
return bit32.rshift(int1, int2);
end

local function bwOr(int1, int2)
if lua_version_int < 53 then
return bit32.bor(int1, int2);
else
return int1.__bor(int2);
end
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
return bit32.bor(int1, int2);
end

local function bwXor(int1, int2)
if lua_version_int < 53 then
return bit32.bxor(int1, int2);
else
return int1.__bxor(int2);
end
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting integer");
assert(int2 and type(int2) == "number" and math.floor(int2) == int2, "Expecting integer");
return bit32.bxor(int1, int2);
end

local function bwNot(int1, int2)
if lua_version_int < 53 then
return bit32.bnot(int1, int2);
else
return int1.__bnot(int2);
end
local function bwNot(int1)
assert(int1 and type(int1) == "number" and math.floor(int1) == int1, "Expecting unsigned");
return bit32.bnot(int1);
end


Expand Down

0 comments on commit c393c9b

Please sign in to comment.