From 031ee8cf9d30d0bae8ceb785a7f2f9a8f901de57 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Mon, 5 Apr 2010 22:10:27 +0300 Subject: [PATCH] math: prevent divide by zero bugs These can happen if their is a real divide by zero bug in the code or if there is a bug in smatch. Signed-off-by: Dan Carpenter --- smatch_math.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/smatch_math.c b/smatch_math.c index 8400bf44..e3be697b 100644 --- a/smatch_math.c +++ b/smatch_math.c @@ -97,7 +97,12 @@ static long long handle_binop(struct expression *expr, int *discard, int *undefi ret = left * right; break; case '/': - ret = left / right; + if (right == 0) { + *undefined = 1; + *discard = 1; + } else { + ret = left / right; + } break; case '+': ret = left + right; @@ -106,7 +111,12 @@ static long long handle_binop(struct expression *expr, int *discard, int *undefi ret = left - right; break; case '%': - ret = left % right; + if (right == 0) { + *undefined = 1; + *discard = 1; + } else { + ret = left % right; + } break; case '|': ret = left | right; -- 2.11.4.GIT