diff options
author | 2013-03-28 08:39:54 +0000 | |
---|---|---|
committer | 2013-03-28 08:39:54 +0000 | |
commit | eb0048bac5daf3bb6590a674d97fd15d090ca073 (patch) | |
tree | 0b6242e8762c954776d069c224301720bfbe56ea | |
parent | Don't die with SIGFPE on LONG_MIN / -1 or % -1. Instead make LONG_MIN / (diff) | |
download | wireguard-openbsd-eb0048bac5daf3bb6590a674d97fd15d090ca073.tar.xz wireguard-openbsd-eb0048bac5daf3bb6590a674d97fd15d090ca073.zip |
Don't die with SIGFPE on INT_MIN / -1 or % -1. Instead make INT_MIN /
-1 == INT_MIN and % -1 == 0.
ok matthew deraadt
-rw-r--r-- | bin/csh/exp.c | 17 | ||||
-rw-r--r-- | bin/expr/expr.c | 11 |
2 files changed, 21 insertions, 7 deletions
diff --git a/bin/csh/exp.c b/bin/csh/exp.c index ffa265fb11b..ded90d6593b 100644 --- a/bin/csh/exp.c +++ b/bin/csh/exp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exp.c,v 1.9 2010/07/20 02:13:10 deraadt Exp $ */ +/* $OpenBSD: exp.c,v 1.10 2013/03/28 08:39:54 nicm Exp $ */ /* $NetBSD: exp.c,v 1.6 1995/03/21 09:02:51 cgd Exp $ */ /*- @@ -32,6 +32,7 @@ #include <sys/types.h> #include <sys/stat.h> +#include <limits.h> #include <stdlib.h> #include <unistd.h> #ifndef SHORT_STRINGS @@ -346,7 +347,7 @@ static Char * exp5(Char ***vp, bool ignore) { Char *p1, *p2; - int i = 0; + int i = 0, l; p1 = exp6(vp, ignore); #ifdef EDEBUG @@ -370,14 +371,22 @@ exp5(Char ***vp, bool ignore) i = egetn(p2); if (i == 0) stderror(ERR_DIV0); - i = egetn(p1) / i; + l = egetn(p1); + if (l == INT_MIN && i == -1) + i = INT_MIN; + else + i = l / i; break; case '%': i = egetn(p2); if (i == 0) stderror(ERR_MOD0); - i = egetn(p1) % i; + l = egetn(p1); + if (l == INT_MIN && i == -1) + i = 0; + else + i = l % i; break; } xfree((ptr_t) p1); diff --git a/bin/expr/expr.c b/bin/expr/expr.c index 615a69ed86d..085ed7114d3 100644 --- a/bin/expr/expr.c +++ b/bin/expr/expr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: expr.c,v 1.17 2006/06/21 18:28:24 deraadt Exp $ */ +/* $OpenBSD: expr.c,v 1.18 2013/03/28 08:40:31 nicm Exp $ */ /* $NetBSD: expr.c,v 1.3.6.1 1996/06/04 20:41:47 cgd Exp $ */ /* @@ -9,6 +9,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <limits.h> #include <locale.h> #include <ctype.h> #include <regex.h> @@ -329,9 +330,13 @@ eval4(void) errx(2, "division by zero"); } if (op == DIV) { - l->u.i /= r->u.i; + if (l->u.i != INT_MIN || r->u.i != -1) + l->u.i /= r->u.i; } else { - l->u.i %= r->u.i; + if (l->u.i != INT_MIN || r->u.i != -1) + l->u.i %= r->u.i; + else + l->u.i = 0; } } |