Skip to contentSkip to conversation

fix SIGFPE caused by signed division overflow

PATCH Applied Xi Wang 2 participants 2 messages May 10, 2013 – May 11, 2013 Sparse checker

lore
Share & tools
lore

Xi Wang

author
#0 lore raw

PATCH 1/1 fix SIGFPE caused by signed division overflow

Avoid evaluating INT_MIN / -1 and INT_MIN % -1, which will trap on x86 and crash sparse.

---
 expand.c         |  2 ++
 simplify.c       |  4 ++++
 validation/div.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 validation/div.c
expand.c +2 -0
index effd27b..2dfa5e5 100644
@@ -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;
 
simplify.c +4 -0
index bda4a5b..b5cd0ea 100644
@@ -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:
@@ -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:
validation/div.c new file +29 -0
new file mode 100644
index 0000000..3dcbfd5
@@ -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
+ */
Signature · 1 line
1.8.1.2
maintainer Applied
#1 lore raw
Quoted 2 lines · Xi Wang

Avoid evaluating INT_MIN / -1 and INT_MIN % -1, which will trap on x86 and crash sparse.

Applied.

Chris