summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Ahern <william@25thandclement.com>2016-12-14 14:25:47 -0800
committerWilliam Ahern <william@25thandclement.com>2016-12-14 14:25:47 -0800
commitb4bf06dcb61dbd735b328f47d8a36afb856d5d16 (patch)
tree47f0cf8d2c22cb7c11c29b05c4a69b088e2b72f2
parentae16dd4dd147404fc73e34ab3263d50d93a57f0b (diff)
downloadluaossl-rel-20161214.tar.gz
luaossl-rel-20161214.tar.bz2
luaossl-rel-20161214.zip
add regression tests after refactoring bignum unary and binary operationsrel-20161214
closes issue #82
-rwxr-xr-xregress/82-bn_prepops-null-deref.lua63
-rw-r--r--regress/regress.lua1
2 files changed, 64 insertions, 0 deletions
diff --git a/regress/82-bn_prepops-null-deref.lua b/regress/82-bn_prepops-null-deref.lua
new file mode 100755
index 0000000..6a1d617
--- /dev/null
+++ b/regress/82-bn_prepops-null-deref.lua
@@ -0,0 +1,63 @@
1#!/usr/bin/env lua
2--
3-- The following code could trigger a NULL dereference.
4--
5-- bn_prepops(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, _Bool commute) {
6-- ...
7-- *b = checkbig(L, 2, &lvalue);
8-- ...
9-- }
10--
11-- bn_sqr(lua_State *L) {
12-- BIGNUM *r, *a;
13--
14-- bn_prepops(L, &r, &a, NULL, 1);
15-- ...
16-- }
17--
18-- Caught by clang static analyzer. This was introduced with a patch adding
19-- the :sqr method. This should have been caught sooner as the :sqr method
20-- couldn't have possibly ever worked--a missing or non-numeric second
21-- operand would have thrown a Lua error, and a numeric second operand
22-- triggers the NULL dereference.
23--
24require"regress".export".*"
25
26local function N(i) return bignum.new(i) end
27
28-- passing a second numeric operand triggered a NULL dereference
29local r = N(4):sqr(0)
30
31
32-- check minimal functionality of all our operators
33local tests = {
34 { op = "add", a = 1, b = 1, r = 2 },
35 { op = "sub", a = 2, b = 1, r = 1 },
36 { op = "mul", a = 2, b = 2, r = 4 },
37 { op = "idiv", a = 4, b = 2, r = 2 },
38 { op = "mod", a = 4, b = 2, r = 0 },
39 { op = "exp", a = 2, b = 2, r = 4 },
40 { op = "sqr", a = 4, b = nil, r = 16 },
41 { op = "gcd", a = 47, b = 3, r = 1 },
42}
43
44local function tdescr(t)
45 return string.format("%s(%s, %s)", t.op, tostring(t.a), tostring(t.b))
46end
47
48for i,t in ipairs(tests) do
49 local a = N(t.a)
50 local op = a[t.op]
51 local ok, r
52
53 if t.b then
54 ok, r = pcall(op, a, t.b)
55 else
56 ok, r = pcall(op, a)
57 end
58
59 check(ok, "failed test #%d (%s) (%s)", i, tdescr(t), r)
60 check(N(r) == N(t.r), "failed test #%d (%s) (expected %s, got %s)", i, tdescr(t), tostring(t.r), tostring(r))
61end
62
63say"OK"
diff --git a/regress/regress.lua b/regress/regress.lua
index 8d955ea..4377db5 100644
--- a/regress/regress.lua
+++ b/regress/regress.lua
@@ -1,5 +1,6 @@
1local regress = { 1local regress = {
2 openssl = require"openssl", 2 openssl = require"openssl",
3 bignum = require"openssl.bignum",
3 pkey = require"openssl.pkey", 4 pkey = require"openssl.pkey",
4 x509 = require"openssl.x509", 5 x509 = require"openssl.x509",
5 name = require"openssl.x509.name", 6 name = require"openssl.x509.name",