comparison: handle divide "a = b / 2"
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 27 Jun 2013 08:07:25 +0000 (27 11:07 +0300)
committerDan Carpenter <dan.carpenter@oracle.com>
Thu, 27 Jun 2013 08:07:25 +0000 (27 11:07 +0300)
If you have code like:

a = b / sizeof(int);

Then "a < b".

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

index 6c136e5..5b3f230 100644 (file)
@@ -605,6 +605,21 @@ static void match_assign_sub(struct expression *expr)
        }
 }
 
+static void match_assign_divide(struct expression *expr)
+{
+       struct expression *right;
+       struct expression *r_left, *r_right;
+       sval_t min;
+
+       right = strip_expr(expr->right);
+       r_left = strip_expr(right->left);
+       r_right = strip_expr(right->right);
+       if (!get_implied_min(r_right, &min) || min.value <= 1)
+               return;
+
+       add_comparison(expr->left, '<', r_left);
+}
+
 static void match_binop_assign(struct expression *expr)
 {
        struct expression *right;
@@ -614,6 +629,8 @@ static void match_binop_assign(struct expression *expr)
                match_assign_add(expr);
        if (right->op == '-')
                match_assign_sub(expr);
+       if (right->op == '/')
+               match_assign_divide(expr);
 }
 
 static void copy_comparisons(struct expression *left, struct expression *right)
diff --git a/validation/sm_compare9.c b/validation/sm_compare9.c
new file mode 100644 (file)
index 0000000..87f772c
--- /dev/null
@@ -0,0 +1,17 @@
+#include "check_debug.h"
+
+void *a, *b;
+static int options_write(void)
+{
+       a = b / 2;
+       __smatch_compare(a, b);
+}
+
+/*
+ * check-name: smatch compare #9
+ * check-command: smatch -I.. sm_compare9.c
+ *
+ * check-output-start
+sm_compare9.c:7 options_write() a < b
+ * check-output-end
+ */