diff mbox

[5/7] warn if testing the address of an array

Message ID 20170322173257.63019-6-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Luc Van Oostenryck March 22, 2017, 5:32 p.m. UTC
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 mbox

Patch

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
+ */