comparison: stop caring so much about wrapping (it's rare)
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 27 Jun 2013 08:03:25 +0000 (27 11:03 +0300)
committerDan Carpenter <dan.carpenter@oracle.com>
Thu, 27 Jun 2013 08:03:25 +0000 (27 11:03 +0300)
If we have code like:

a = b + 1;

Then it's possible that "b + 1" might wrap, but it's more likely that it
won't so lets say that "a > b".

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
smatch_comparison.c
validation/sm_compare8.c [new file with mode: 0644]

index 7e13c35..6c136e5 100644 (file)
@@ -564,29 +564,18 @@ static void match_assign_add(struct expression *expr)
        r_left = strip_expr(right->left);
        r_right = strip_expr(right->right);
 
-       if (!is_capped(expr->left)) {
-               get_absolute_max(r_left, &left_tmp);
-               get_absolute_max(r_right, &right_tmp);
-               if (sval_binop_overflows(left_tmp, '+', right_tmp))
-                       return;
-       }
-
        get_absolute_min(r_left, &left_tmp);
-       if (sval_is_negative(left_tmp))
-               return;
        get_absolute_min(r_right, &right_tmp);
-       if (sval_is_negative(right_tmp))
-               return;
 
-       if (left_tmp.value == 0)
-               add_comparison(expr->left, SPECIAL_GTE, r_right);
-       else
+       if (left_tmp.value > 0)
                add_comparison(expr->left, '>', r_right);
+       else if (left_tmp.value == 0)
+               add_comparison(expr->left, SPECIAL_GTE, r_right);
 
-       if (right_tmp.value == 0)
-               add_comparison(expr->left, SPECIAL_GTE, r_left);
-       else
+       if (right_tmp.value > 0)
                add_comparison(expr->left, '>', r_left);
+       else if (right_tmp.value == 0)
+               add_comparison(expr->left, SPECIAL_GTE, r_left);
 }
 
 static void match_assign_sub(struct expression *expr)
diff --git a/validation/sm_compare8.c b/validation/sm_compare8.c
new file mode 100644 (file)
index 0000000..87b5d64
--- /dev/null
@@ -0,0 +1,17 @@
+#include "check_debug.h"
+
+void *a, *b;
+static int options_write(void)
+{
+       a = b + 1;
+       __smatch_compare(a, b);
+}
+
+/*
+ * check-name: smatch compare #8
+ * check-command: smatch -I.. sm_compare8.c
+ *
+ * check-output-start
+sm_compare8.c:7 options_write() a > b
+ * check-output-end
+ */