Skip to content

Commit

Permalink
add regression tests after refactoring bignum unary and binary operat…
Browse files Browse the repository at this point in the history
…ions

closes issue #82
  • Loading branch information
wahern committed Dec 14, 2016
1 parent ae16dd4 commit b4bf06d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions regress/82-bn_prepops-null-deref.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env lua
--
-- The following code could trigger a NULL dereference.
--
-- bn_prepops(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, _Bool commute) {
-- ...
-- *b = checkbig(L, 2, &lvalue);
-- ...
-- }
--
-- bn_sqr(lua_State *L) {
-- BIGNUM *r, *a;
--
-- bn_prepops(L, &r, &a, NULL, 1);
-- ...
-- }
--
-- Caught by clang static analyzer. This was introduced with a patch adding
-- the :sqr method. This should have been caught sooner as the :sqr method
-- couldn't have possibly ever worked--a missing or non-numeric second
-- operand would have thrown a Lua error, and a numeric second operand
-- triggers the NULL dereference.
--
require"regress".export".*"

local function N(i) return bignum.new(i) end

-- passing a second numeric operand triggered a NULL dereference
local r = N(4):sqr(0)


-- check minimal functionality of all our operators
local tests = {
{ op = "add", a = 1, b = 1, r = 2 },
{ op = "sub", a = 2, b = 1, r = 1 },
{ op = "mul", a = 2, b = 2, r = 4 },
{ op = "idiv", a = 4, b = 2, r = 2 },
{ op = "mod", a = 4, b = 2, r = 0 },
{ op = "exp", a = 2, b = 2, r = 4 },
{ op = "sqr", a = 4, b = nil, r = 16 },
{ op = "gcd", a = 47, b = 3, r = 1 },
}

local function tdescr(t)
return string.format("%s(%s, %s)", t.op, tostring(t.a), tostring(t.b))
end

for i,t in ipairs(tests) do
local a = N(t.a)
local op = a[t.op]
local ok, r

if t.b then
ok, r = pcall(op, a, t.b)
else
ok, r = pcall(op, a)
end

check(ok, "failed test #%d (%s) (%s)", i, tdescr(t), r)
check(N(r) == N(t.r), "failed test #%d (%s) (expected %s, got %s)", i, tdescr(t), tostring(t.r), tostring(r))
end

say"OK"
1 change: 1 addition & 0 deletions regress/regress.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local regress = {
openssl = require"openssl",
bignum = require"openssl.bignum",
pkey = require"openssl.pkey",
x509 = require"openssl.x509",
name = require"openssl.x509.name",
Expand Down

0 comments on commit b4bf06d

Please sign in to comment.