diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-12-15 06:39:51 +0100 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-12-17 23:19:34 +0100 |
commit | 6002ded74587a1469fcd930dd46dc89eb3307982 (patch) | |
tree | e508674afaf319fe04d2b08e73c6f50fbfa82cc7 | |
parent | 45503a2bb1cbd400fe5aa01041cae3bf484b205b (diff) | |
download | sparse-6002ded74587a1469fcd930dd46dc89eb3307982.tar.gz |
add a flag to warn on casts to/from bitwise pointers
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 (defaulting to off as it creates tens of
thousands warnings in the kernel).
CC: Thiebaud Weksteen <tweek@google.com>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r-- | evaluate.c | 19 | ||||
-rw-r--r-- | lib.c | 2 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | sparse.1 | 6 | ||||
-rw-r--r-- | validation/bitwise-cast-ptr.c | 3 |
5 files changed, 29 insertions, 2 deletions
@@ -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; } @@ -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 }, @@ -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; @@ -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 acf0ac35..3f2bfdaf 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) |