diff mbox

[3/7] warn if testing the address of a function

Message ID 20170322173257.63019-4-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 a function 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).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                         |  5 ++++-
 validation/cond-address-function.c | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 validation/cond-address-function.c
diff mbox

Patch

diff --git a/evaluate.c b/evaluate.c
index 47eeaef2e..d8ec1c2e3 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -869,7 +869,10 @@  static struct symbol *evaluate_conditional(struct expression *expr, int iterator
 	if (ctype) {
 		if (is_safe_type(ctype))
 			warning(expr->pos, "testing a 'safe expression'");
-		if (!is_scalar_type(ctype)) {
+		if (is_func_type(ctype)) {
+			if (Waddress)
+				warning(expr->pos, "the address of %s will always evaluate as true", "a function");
+		} else if (!is_scalar_type(ctype)) {
 			sparse_error(expr->pos, "incorrect type in conditional");
 			info(expr->pos, "   got %s", show_typename(ctype));
 			ctype = NULL;
diff --git a/validation/cond-address-function.c b/validation/cond-address-function.c
new file mode 100644
index 000000000..9a143a009
--- /dev/null
+++ b/validation/cond-address-function.c
@@ -0,0 +1,18 @@ 
+extern void func(void);
+
+int global_function(void)
+{
+	if (func)
+		return 1;
+	return 0;
+}
+
+/*
+ * check-name: cond-address-function
+ * check-command: test-linearize -Wno-decl -Waddress $file
+ * check-output-ignore
+ *
+ * check-error-start
+cond-address-function.c:5:13: warning: the address of a function will always evaluate as true
+ * check-error-end
+ */