From e90d623b488b1cfee3831c5a50aefb717fe94553 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Thu, 5 Sep 2013 22:53:45 +0300 Subject: [PATCH] math: handle bitwise OR Avoid some false positives about integer wrapping bugs. Signed-off-by: Dan Carpenter --- smatch_math.c | 20 ++++++++++++++++++++ validation/sm_math2.c | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 validation/sm_math2.c diff --git a/smatch_math.c b/smatch_math.c index ee6943f9..023aa200 100644 --- a/smatch_math.c +++ b/smatch_math.c @@ -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 index 00000000..640dc90a --- /dev/null +++ b/validation/sm_math2.c @@ -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 + */ -- 2.11.4.GIT