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>,
	Christopher Li <sparse@chrisli.org>
Subject: [PATCH 5/7] warn if testing the address of an array
Date: Wed, 22 Mar 2017 18:32:55 +0100	[thread overview]
Message-ID: <20170322173257.63019-6-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170322173257.63019-1-luc.vanoostenryck@gmail.com>

Testing the address of an array is quite suspicious:
it's most probably the sign of an error somewhere.
Furthermore, such uses always evaluate to true.

So, add a warning about such use (but only if -Waddress was given).
---
 evaluate.c                      |  3 +++
 validation/cond-address-array.c | 26 ++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 validation/cond-address-array.c

diff --git a/evaluate.c b/evaluate.c
index d8ec1c2e3..f2d95c79d 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -872,6 +872,9 @@ static struct symbol *evaluate_conditional(struct expression *expr, int iterator
 		if (is_func_type(ctype)) {
 			if (Waddress)
 				warning(expr->pos, "the address of %s will always evaluate as true", "a function");
+		} else if (is_array_type(ctype)) {
+			if (Waddress)
+				warning(expr->pos, "the address of %s will always evaluate as true", "an array");
 		} else if (!is_scalar_type(ctype)) {
 			sparse_error(expr->pos, "incorrect type in conditional");
 			info(expr->pos, "   got %s", show_typename(ctype));
diff --git a/validation/cond-address-array.c b/validation/cond-address-array.c
new file mode 100644
index 000000000..e1d2f87f8
--- /dev/null
+++ b/validation/cond-address-array.c
@@ -0,0 +1,26 @@
+int foo(void) {
+	extern int a[];
+
+	if (a)
+		return 1;
+	return 0;
+}
+
+int bar(void) {
+	int a[2];
+
+	if (a)
+		return 1;
+	return 0;
+}
+
+/*
+ * check-name: cond-address-array.c
+ * check-command: test-linearize -Wno-decl -Waddress $file
+ * check-output-ignore
+ *
+ * check-error-start
+cond-address-array.c:4:13: warning: the address of an array will always evaluate as true
+cond-address-array.c:12:13: warning: the address of an array will always evaluate as true
+ * check-error-end
+ */
-- 
2.12.0


  parent reply	other threads:[~2017-03-22 17:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-22 17:32 [PATCH 0/7] fix function or array address in conditionals Luc Van Oostenryck
2017-03-22 17:32 ` [PATCH 1/7] teach sparse about -Waddress Luc Van Oostenryck
2017-03-22 17:32 ` [PATCH 2/7] add is_func_type() Luc Van Oostenryck
2017-03-22 17:32 ` [PATCH 3/7] warn if testing the address of a function Luc Van Oostenryck
2017-03-22 17:32 ` [PATCH 4/7] add is_array_type() Luc Van Oostenryck
2017-03-22 17:32 ` Luc Van Oostenryck [this message]
2017-03-22 17:32 ` [PATCH 6/7] fix evaluation of a function or array symbol in conditionals Luc Van Oostenryck
2017-03-22 17:32 ` [PATCH 7/7] fix is_scalar_type() Luc Van Oostenryck
2017-04-01 10:36 ` [GIT PULL] fix function or array address in conditionals 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=20170322173257.63019-6-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.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).