math: handle bitwise OR
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 5 Sep 2013 19:53:45 +0000 (5 22:53 +0300)
committerDan Carpenter <dan.carpenter@oracle.com>
Thu, 5 Sep 2013 19:53:45 +0000 (5 22:53 +0300)
Avoid some false positives about integer wrapping bugs.

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

index ee6943f..023aa20 100644 (file)
@@ -306,6 +306,24 @@ static struct range_list *handle_bitwise_AND(struct expression *expr, int implie
        return rl_intersection(left_rl, right_rl);
 }
 
+static struct range_list *handle_bitwise_OR(struct expression *expr, int implied)
+{
+       struct symbol *type;
+       struct range_list *left_rl, *right_rl;
+
+       if (implied != RL_IMPLIED && implied != RL_ABSOLUTE)
+               return NULL;
+
+       type = get_type(expr);
+
+       get_absolute_rl(expr->left, &left_rl);
+       get_absolute_rl(expr->right, &right_rl);
+       left_rl = cast_rl(type, left_rl);
+       right_rl = cast_rl(type, right_rl);
+
+       return rl_union(left_rl, right_rl);
+}
+
 static struct range_list *handle_right_shift(struct expression *expr, int implied)
 {
        struct range_list *left_rl;
@@ -398,6 +416,8 @@ static struct range_list *handle_binop_rl(struct expression *expr, int implied)
                return handle_mod_rl(expr, implied);
        case '&':
                return handle_bitwise_AND(expr, implied);
+       case '|':
+               return handle_bitwise_OR(expr, implied);
        case SPECIAL_RIGHTSHIFT:
                return handle_right_shift(expr, implied);
        case SPECIAL_LEFTSHIFT:
diff --git a/validation/sm_math2.c b/validation/sm_math2.c
new file mode 100644 (file)
index 0000000..640dc90
--- /dev/null
@@ -0,0 +1,25 @@
+#include "check_debug.h"
+
+unsigned char buf[2];
+
+void test(void)
+{
+       int a = buf[1];
+       int b = buf[0] << 8;
+       int c = (buf[0] << 8) | buf[1];
+
+       __smatch_implied(a);
+       __smatch_implied(b);
+       __smatch_implied(c);
+}
+
+/*
+ * check-name: smatch math #2
+ * check-command: smatch -I.. sm_math2.c
+ *
+ * check-output-start
+sm_math2.c:11 test() implied: a = '0-255'
+sm_math2.c:12 test() implied: b = '0,256-65280'
+sm_math2.c:13 test() implied: c = '0-65280'
+ * check-output-end
+ */