diff mbox

Add testcases for bitwise cast on pointer

Message ID 20180425120217.90744-1-tweek@google.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

ThiƩbaud Weksteen April 25, 2018, 12:02 p.m. UTC
Signed-off-by: Thiebaud Weksteen <tweek@google.com>
---
 validation/bitwise-cast.c | 46 +++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 9 deletions(-)

Comments

Luc Van Oostenryck April 26, 2018, 7:22 p.m. UTC | #1
Thanks for the patch.
I've changed it a bit though, putting the pointers part in
a different fine, in order to not mix the tests with simple
__bitwise types (which are OK) with those on __bitwise pointers
(which fail). You can see the result on:
	git://github.com/lucvoo/sparse-dev.git bug-restricted-ptr


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

Patch

diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
index baeca29..8eaabb7 100644
--- a/validation/bitwise-cast.c
+++ b/validation/bitwise-cast.c
@@ -18,8 +18,6 @@  static __be32 bar(void)
 /* Implicit casts of nonzero, bad */
 static __be32 baz(void)
 {
-	__be32 x = 0x2a;
-
 	return 99;
 }
 
@@ -29,16 +27,46 @@  static __be32 quux(void)
 	return (__be32)1729;
 }
 
+/* Explicit case of nonzero forced, legal */
+static __be32 quuy(void)
+{
+	return (__attribute__((force)) __be32) 1730;
+}
+
+static u32 global_i = 31337;
+
+/* Implicit cast of pointer, bad */
+static __be32* quuz(void)
+{
+	u32* x = &global_i;
+	return x;
+}
+
+/* Explicit cast of pointer, bad */
+static __be32* quva(void)
+{
+	u32* x = &global_i;
+	return (__be32 *) x;
+}
+
+/* Explicit cast of pointer forced, legal */
+static __be32* quvb(void)
+{
+	u32* x = &global_i;
+	return (__attribute__((force)) __be32 *) x;
+}
+
 /*
  * check-name: conversions to bitwise types
  * check-command: sparse -Wbitwise $file
  * check-error-start
-bitwise-cast.c:21:20: warning: incorrect type in initializer (different base types)
-bitwise-cast.c:21:20:    expected restricted __be32 [usertype] x
-bitwise-cast.c:21:20:    got int
-bitwise-cast.c:23:16: warning: incorrect type in return expression (different base types)
-bitwise-cast.c:23:16:    expected restricted __be32
-bitwise-cast.c:23:16:    got int
-bitwise-cast.c:29:17: warning: cast to restricted __be32
+bitwise-cast.c:22:16: warning: incorrect type in return expression (different base types)
+bitwise-cast.c:22:16:    expected restricted __be32
+bitwise-cast.c:22:16:    got int
+bitwise-cast.c:28:17: warning: cast to restricted __be32
+bitwise-cast.c:43:16: warning: incorrect type in return expression (different base types)
+bitwise-cast.c:43:16:    expected restricted __be32 [usertype] *
+bitwise-cast.c:43:16:    got unsigned int [usertype] *x
+bitwise-cast.c:50:17: warning: cast to restricted __be32 *
  * check-error-end
  */