[PATCH v2] Allow casting to a restricted type if !restricted_value

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

 



If the operand of a cast to a restricted type is an unrestricted value,
 the cast should not produce a warning, since an equivalent implied cast
 (e.g. in an initialiser) would not do so.

Also adds a test case (bitwise-cast.c) testing implicit and explicit
 conversions of zero and nonzero integers to bitwise type.

Signed-off-by: Edward Cree <ecree@xxxxxxxxxxxxxx>
---
v2: combined change and test case into one patch;
    removed RFC tags.

 evaluate.c                |  2 +-
 validation/bitwise-cast.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 validation/bitwise-cast.c

diff --git a/evaluate.c b/evaluate.c
index e350c0c..8e855b1 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2771,7 +2771,7 @@ static struct symbol *evaluate_cast(struct expression *expr)
 		t2 = unfoul(t2);
 
 	if (t1 != t2) {
-		if (class1 & TYPE_RESTRICT)
+		if ((class1 & TYPE_RESTRICT) && restricted_value(target, t1))
 			warning(expr->pos, "cast to %s",
 				show_typename(t1));
 		if (class2 & TYPE_RESTRICT)
diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
new file mode 100644
index 0000000..baeca29
--- /dev/null
+++ b/validation/bitwise-cast.c
@@ -0,0 +1,44 @@
+typedef unsigned int u32;
+typedef u32 __attribute__((bitwise)) __be32;
+
+/* Implicit casts of 0, legal */
+static __be32 foo(void)
+{
+	__be32 x = 0;
+
+	return 0;
+}
+
+/* Explicit cast of 0, legal */
+static __be32 bar(void)
+{
+	return (__be32)0;
+}
+
+/* Implicit casts of nonzero, bad */
+static __be32 baz(void)
+{
+	__be32 x = 0x2a;
+
+	return 99;
+}
+
+/* Explicit cast of nonzero, bad */
+static __be32 quux(void)
+{
+	return (__be32)1729;
+}
+
+/*
+ * 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
+ * check-error-end
+ */
--
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