Skip to content

Commit

Permalink
fix SIGFPE caused by signed division overflow
Browse files Browse the repository at this point in the history
Avoid evaluating INT_MIN / -1 and INT_MIN % -1, which will trap on x86
and crash sparse.

Signed-off-by: Xi Wang <xi.wang@gmail.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
  • Loading branch information
xiw authored and sparsecli committed May 11, 2013
1 parent 5449cfb commit 652eb80
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions expand.c
Expand Up @@ -239,6 +239,8 @@ static int simplify_int_binop(struct expression *expr, struct symbol *ctype)
case SIGNED('%'):
if (!r)
goto Div;
if (l == mask && sr == -1)
goto Overflow;
v = sl % sr;
break;

Expand Down
4 changes: 4 additions & 0 deletions simplify.c
Expand Up @@ -406,6 +406,8 @@ static int simplify_constant_binop(struct instruction *insn)
case OP_DIVS:
if (!right)
return 0;
if (left == mask && right == -1)
return 0;
res = left / right;
break;
case OP_MODU:
Expand All @@ -416,6 +418,8 @@ static int simplify_constant_binop(struct instruction *insn)
case OP_MODS:
if (!right)
return 0;
if (left == mask && right == -1)
return 0;
res = left % right;
break;
case OP_SHL:
Expand Down
29 changes: 29 additions & 0 deletions validation/div.c
@@ -0,0 +1,29 @@
#include <limits.h>

static int xd = 1 / 0;
static int xl = 1L / 0;
static int xll = 1LL / 0;

static int yd = INT_MIN / -1;
static long yl = LONG_MIN / -1;
static long long yll = LLONG_MIN / -1;

static int zd = INT_MIN % -1;
static long zl = LONG_MIN % -1;
static long long zll = LLONG_MIN % -1;

/*
* check-name: division constants
*
* check-error-start
div.c:3:19: warning: division by zero
div.c:4:20: warning: division by zero
div.c:5:22: warning: division by zero
div.c:7:25: warning: constant integer operation overflow
div.c:8:27: warning: constant integer operation overflow
div.c:9:34: warning: constant integer operation overflow
div.c:11:25: warning: constant integer operation overflow
div.c:12:27: warning: constant integer operation overflow
div.c:13:34: warning: constant integer operation overflow
* check-error-end
*/

0 comments on commit 652eb80

Please sign in to comment.