From 4480ef07d1187395ea8b277174ac2545254b40f0 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Wed, 22 Mar 2017 17:27:22 +0100 Subject: [PATCH] warn if testing the address of an array 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 d8ec1c2e..f2d95c79 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 00000000..e1d2f87f --- /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.11.4.GIT