[PATCH] also report type & size when warning on non-power-of-2 pointer subtraction

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

 



The option -Wptr-subtraction-blows warns when pointer subtraction
is done and the base size is not a power-of-2.
However, the current message doesn't give much context.

Change this by giving the base type and the size in the warning.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 evaluate.c                 |  2 +-
 validation/ptr-sub-blows.c | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 validation/ptr-sub-blows.c

diff --git a/evaluate.c b/evaluate.c
index 4be4e8e5b..7818af48f 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -848,7 +848,7 @@ static struct symbol *evaluate_ptr_sub(struct expression *expr)
 
 		if (value & (value-1)) {
 			if (Wptr_subtraction_blows)
-				warning(expr->pos, "potentially expensive pointer subtraction");
+				warning(expr->pos, "potentially expensive pointer subtraction (size = %lu for %s)", value, show_typename(lbase));
 		}
 
 		sub->op = '-';
diff --git a/validation/ptr-sub-blows.c b/validation/ptr-sub-blows.c
new file mode 100644
index 000000000..433bd4e0c
--- /dev/null
+++ b/validation/ptr-sub-blows.c
@@ -0,0 +1,22 @@
+static int ok(int *a, int *b)
+{
+	return a - b;
+}
+
+struct s {
+	int a, b, c;
+};
+
+static int ko(struct s *a, struct s *b)
+{
+	return a - b;
+}
+
+/*
+ * check-name: ptr-sub-blows
+ * check-command: sparse -Wptr-subtraction-blows $file
+ *
+ * check-error-start
+ptr-sub-blows.c:12:18: warning: potentially expensive pointer subtraction (size = 12 for struct s)
+ * check-error-end
+ */
-- 
2.16.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