[PATCH] add a flag to warn on casts to/from bitwise pointers

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

 



Support for 'bitwise' integers is one of the main sparse's extension.

However, casts to or from pointers to bitwise types can be done
without incurring any sort of warnings although such casts can
be as wrong as direct casts to or from bitwise integers themselves.

Add the corresponding warnings and control them by a new flag
-Wbitwise-pointer (dzafualting to off as it creates tens of
thousands of warnings in the kernel).

CC: Thiebaud Weksteen <tweek@xxxxxxxxxx>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 evaluate.c                    | 19 +++++++++++++++++++
 lib.c                         |  2 ++
 lib.h                         |  1 +
 sparse.1                      |  6 ++++++
 validation/bitwise-cast-ptr.c |  3 +--
 5 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index 64c02564f..4c8c002ee 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -3045,6 +3045,25 @@ static struct symbol *evaluate_cast(struct expression *expr)
 	if (ttype == &bool_ctype)
 		cast_to_bool(expr);
 
+	// checks pointers to restricted
+	while (Wbitwise_pointer && tclass == TYPE_PTR && sclass == TYPE_PTR) {
+		tclass = classify_type(ttype->ctype.base_type, &ttype);
+		sclass = classify_type(stype->ctype.base_type, &stype);
+		if (ttype == stype)
+			break;
+		if (!ttype || !stype)
+			break;
+		if (ttype == &void_ctype || stype == &void_ctype)
+			break;
+		if (tclass & TYPE_RESTRICT) {
+			warning(expr->pos, "cast to %s", show_typename(ctype));
+			break;
+		}
+		if (sclass & TYPE_RESTRICT) {
+			warning(expr->pos, "cast from %s", show_typename(source->ctype));
+			break;
+		}
+	}
 out:
 	return ctype;
 }
diff --git a/lib.c b/lib.c
index b981bf642..3a9962a88 100644
--- a/lib.c
+++ b/lib.c
@@ -251,6 +251,7 @@ static struct token *pre_buffer_end = NULL;
 int Waddress = 0;
 int Waddress_space = 1;
 int Wbitwise = 1;
+int Wbitwise_pointer = 0;
 int Wcast_from_as = 0;
 int Wcast_to_as = 0;
 int Wcast_truncate = 1;
@@ -692,6 +693,7 @@ static const struct flag warnings[] = {
 	{ "address", &Waddress },
 	{ "address-space", &Waddress_space },
 	{ "bitwise", &Wbitwise },
+	{ "bitwise-pointer", &Wbitwise_pointer},
 	{ "cast-from-as", &Wcast_from_as },
 	{ "cast-to-as", &Wcast_to_as },
 	{ "cast-truncate", &Wcast_truncate },
diff --git a/lib.h b/lib.h
index 743150763..0e2a923bc 100644
--- a/lib.h
+++ b/lib.h
@@ -139,6 +139,7 @@ extern int preprocess_only;
 extern int Waddress;
 extern int Waddress_space;
 extern int Wbitwise;
+extern int Wbitwise_pointer;
 extern int Wcast_from_as;
 extern int Wcast_to_as;
 extern int Wcast_truncate;
diff --git a/sparse.1 b/sparse.1
index ccee2808e..096c5b08c 100644
--- a/sparse.1
+++ b/sparse.1
@@ -77,6 +77,12 @@ Sparse issues these warnings by default.  To turn them off, use
 \fB\-Wno\-bitwise\fR.
 .
 .TP
+.B \-Wbitwise\-pointer
+Same as \fB\-Wbitwise\fR but for casts to or from pointers to bitwise types.
+
+Sparse does not issue these warnings by default.
+.
+.TP
 .B \-Wcast\-from\-as
 Warn about which remove an address space to a pointer type.
 
diff --git a/validation/bitwise-cast-ptr.c b/validation/bitwise-cast-ptr.c
index c390092ee..77927f49b 100644
--- a/validation/bitwise-cast-ptr.c
+++ b/validation/bitwise-cast-ptr.c
@@ -22,8 +22,7 @@ static __be32* tobf(u32 *x)
 
 /*
  * check-name: cast of bitwise pointers
- * check-command: sparse -Wbitwise $file
- * check-known-to-fail
+ * check-command: sparse -Wbitwise -Wbitwise-pointer $file
  *
  * check-error-start
 bitwise-cast-ptr.c:9:16: warning: incorrect type in return expression (different base types)
-- 
2.20.0




[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