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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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 --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
+ */
-- 
2.12.0

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux