test_overflow: macrofy some more, do more tests for free
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Mon, 4 Jun 2018 06:41:27 +0000 (08:41 +0200)
committerKees Cook <keescook@chromium.org>
Tue, 5 Jun 2018 19:16:51 +0000 (12:16 -0700)
Obviously a+b==b+a and a*b==b*a, but the implementation of the fallback
checks are not entirely symmetric in how they treat a and b. So we might
as well check the (b,a,r,of) tuple as well as the (a,b,r,of) one for +
and *. Rather than more copy-paste, factor out the common part to
check_one_op.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Kees Cook <keescook@chromium.org>
lib/test_overflow.c

index af01e46500be7fac1f342e948d13c487d62b822b..fa87e283b1490f5fefd0ebd11873a23b2d96f958 100644 (file)
@@ -211,35 +211,31 @@ DEFINE_TEST_ARRAY(s64) = {
        {0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
 };
 
+#define check_one_op(t, fmt, op, sym, a, b, r, of) do {                \
+       t _r;                                                   \
+       bool _of;                                               \
+                                                               \
+       _of = check_ ## op ## _overflow(a, b, &_r);             \
+       if (_of != of) {                                        \
+               pr_warn("expected "fmt" "sym" "fmt              \
+                       " to%s overflow (type %s)\n",           \
+                       a, b, of ? "" : " not", #t);            \
+       }                                                       \
+       if (_r != r) {                                          \
+               pr_warn("expected "fmt" "sym" "fmt" == "        \
+                       fmt", got "fmt" (type %s)\n",           \
+                       a, b, r, _r, #t);                       \
+       }                                                       \
+} while (0)
+
 #define DEFINE_TEST_FUNC(t, fmt)                                       \
 static void __init do_test_ ## t(const struct test_ ## t *p)           \
 {                                                                      \
-       t r;                                                            \
-       bool of;                                                        \
-                                                                       \
-       of = check_add_overflow(p->a, p->b, &r);                        \
-       if (of != p->s_of)                                              \
-               pr_warn("expected "fmt" + "fmt" to%s overflow (type %s)\n", \
-                       p->a, p->b, p->s_of ? "" : " not", #t);         \
-       if (r != p->sum)                                                \
-               pr_warn("expected "fmt" + "fmt" == "fmt", got "fmt" (type %s)\n", \
-                       p->a, p->b, p->sum, r, #t);                     \
-                                                                       \
-       of = check_sub_overflow(p->a, p->b, &r);                        \
-       if (of != p->d_of)                                              \
-               pr_warn("expected "fmt" - "fmt" to%s overflow (type %s)\n", \
-                       p->a, p->b, p->s_of ? "" : " not", #t);         \
-       if (r != p->diff)                                               \
-               pr_warn("expected "fmt" - "fmt" == "fmt", got "fmt" (type %s)\n", \
-                       p->a, p->b, p->diff, r, #t);                    \
-                                                                       \
-       of = check_mul_overflow(p->a, p->b, &r);                        \
-       if (of != p->p_of)                                              \
-               pr_warn("expected "fmt" * "fmt" to%s overflow (type %s)\n", \
-                       p->a, p->b, p->p_of ? "" : " not", #t);         \
-       if (r != p->prod)                                               \
-               pr_warn("expected "fmt" * "fmt" == "fmt", got "fmt" (type %s)\n", \
-                       p->a, p->b, p->prod, r, #t);                    \
+       check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of);    \
+       check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of);    \
+       check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of);   \
+       check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of);   \
+       check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of);   \
 }                                                                      \
                                                                        \
 static void __init test_ ## t ## _overflow(void) {                     \