linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 8/9] div0: warn on float divide by 0 also when the lhs is not constant
Date: Wed, 31 May 2017 05:22:06 +0200	[thread overview]
Message-ID: <20170531032207.95830-9-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170531032207.95830-1-luc.vanoostenryck@gmail.com>

The current code detects and warns on division by zero but
only when the left-hand side is a constant value.

Fix that by moving up the code which detect such divisions
before checking if the LHS is a constant.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c                    | 18 ++++++++++--------
 validation/div-by-zero-fp.c | 16 ++++++++++++++++
 2 files changed, 26 insertions(+), 8 deletions(-)
 create mode 100644 validation/div-by-zero-fp.c

diff --git a/expand.c b/expand.c
index 8fd258e25..dacee7a76 100644
--- a/expand.c
+++ b/expand.c
@@ -325,19 +325,23 @@ static int simplify_float_binop(struct expression *expr)
 	unsigned long mod = expr->ctype->ctype.modifiers;
 	long double l, r, res;
 
-	if (left->type != EXPR_FVALUE || right->type != EXPR_FVALUE)
+	if (right->type != EXPR_FVALUE)
 		return 0;
 
-	l = left->fvalue;
 	r = right->fvalue;
+	if (!r && expr->op == '/')
+		goto Div;
+
+	if (left->type != EXPR_FVALUE)
+		return 0;
+	l = left->fvalue;
 
 	if (mod & MOD_LONGLONG) {
 		switch (expr->op) {
 		case '+':	res = l + r; break;
 		case '-':	res = l - r; break;
 		case '*':	res = l * r; break;
-		case '/':	if (!r) goto Div;
-				res = l / r; break;
+		case '/':	res = l / r; break;
 		default: return 0;
 		}
 	} else if (mod & MOD_LONG) {
@@ -345,8 +349,7 @@ static int simplify_float_binop(struct expression *expr)
 		case '+':	res = (double) l + (double) r; break;
 		case '-':	res = (double) l - (double) r; break;
 		case '*':	res = (double) l * (double) r; break;
-		case '/':	if (!r) goto Div;
-				res = (double) l / (double) r; break;
+		case '/':	res = (double) l / (double) r; break;
 		default: return 0;
 		}
 	} else {
@@ -354,8 +357,7 @@ static int simplify_float_binop(struct expression *expr)
 		case '+':	res = (float)l + (float)r; break;
 		case '-':	res = (float)l - (float)r; break;
 		case '*':	res = (float)l * (float)r; break;
-		case '/':	if (!r) goto Div;
-				res = (float)l / (float)r; break;
+		case '/':	res = (float)l / (float)r; break;
 		default: return 0;
 		}
 	}
diff --git a/validation/div-by-zero-fp.c b/validation/div-by-zero-fp.c
new file mode 100644
index 000000000..787af1636
--- /dev/null
+++ b/validation/div-by-zero-fp.c
@@ -0,0 +1,16 @@
+double fbad(double a) { return 2.0 / 0; }
+double gbad(double a) { return 2.0 / 0.0; }
+double fool(double a) { return a / 0; }
+double ffoo(double a) { return a / 0.0; }
+
+/*
+ * check-name: div-by-zero-fp.c
+ * check-command: sparse -Wno-decl $file
+ *
+ * check-error-start
+div-by-zero-fp.c:1:36: warning: division by zero
+div-by-zero-fp.c:2:36: warning: division by zero
+div-by-zero-fp.c:3:34: warning: division by zero
+div-by-zero-fp.c:4:34: warning: division by zero
+ * check-error-end
+ */
-- 
2.13.0


  parent reply	other threads:[~2017-05-31  3:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-31  3:21 [PATCH 0/9] division by zero warnings Luc Van Oostenryck
2017-05-31  3:21 ` [PATCH 1/9] add is_pseudo_value() Luc Van Oostenryck
2017-05-31  3:22 ` [PATCH 2/9] add a .warned field to struct instruction Luc Van Oostenryck
2017-05-31  3:22 ` [PATCH 3/9] div0: warn on integer divide by 0 also when the lhs is not constant Luc Van Oostenryck
2017-05-31  3:22 ` [PATCH 4/9] div0: warn on division by zero - general case Luc Van Oostenryck
2017-05-31  3:22 ` [PATCH 5/9] div0: add warning option -Wdiv-by-zero Luc Van Oostenryck
2017-05-31  3:22 ` [PATCH 6/9] div0: use -Wdiv-by-zero Luc Van Oostenryck
2017-05-31  3:22 ` [PATCH 7/9] div0: warn also during simplification Luc Van Oostenryck
2017-05-31  3:22 ` Luc Van Oostenryck [this message]
2017-05-31  3:22 ` [PATCH 9/9] div0: add missing tests for floating point div by zero Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170531032207.95830-9-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).