From: Dan Carpenter Date: Thu, 27 Jun 2013 08:07:25 +0000 (+0300) Subject: comparison: handle divide "a = b / 2" X-Git-Tag: 1.59~33 X-Git-Url: https://repo.or.cz/w/smatch.git/commitdiff_plain/ee4cbd5ded1e2ebb4d987d5e76639672af9a0e64 comparison: handle divide "a = b / 2" If you have code like: a = b / sizeof(int); Then "a < b". Signed-off-by: Dan Carpenter --- diff --git a/smatch_comparison.c b/smatch_comparison.c index 6c136e51..5b3f2305 100644 --- a/smatch_comparison.c +++ b/smatch_comparison.c @@ -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 index 00000000..87f772c5 --- /dev/null +++ b/validation/sm_compare9.c @@ -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 + */