Skip to content
Permalink
Browse files

stricter warning for explicit cast to ulong

sparse issues a warning when user pointers are casted to integer
types except to unsigned longs which are explicitly allowed.
However it may happen that we would like to also be warned
on casts to unsigned long.

Fix this by adding a new warning flag: -Wcast-from-as (to mirrors
-Wcast-to-as) which extends -Waddress-space to all casts that
remove an address space attribute (without using __force).

References: https://lore.kernel.org/lkml/20180628102741.vk6vphfinlj3lvhv@armageddon.cambridge.arm.com/
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
  • Loading branch information...
lucvoo committed Jun 28, 2018
1 parent 1ed0dfb commit 5f960cb10f56ec2017c128ef9d16060e0145f292
Showing with 70 additions and 2 deletions.
  1. +2 −2 evaluate.c
  2. +2 −0 lib.c
  3. +1 −0 lib.h
  4. +9 −0 sparse.1
  5. +56 −0 validation/Waddress-space-strict.c
@@ -2998,14 +2998,14 @@ static struct symbol *evaluate_cast(struct expression *expr)
}
}

if (ttype == &ulong_ctype)
if (ttype == &ulong_ctype && !Wcast_from_as)
tas = -1;
else if (tclass == TYPE_PTR) {
examine_pointer_target(ttype);
tas = ttype->ctype.as;
}

if (stype == &ulong_ctype)
if (stype == &ulong_ctype && !Wcast_from_as)
sas = -1;
else if (sclass == TYPE_PTR) {
examine_pointer_target(stype);
2 lib.c
@@ -248,6 +248,7 @@ static struct token *pre_buffer_end = NULL;
int Waddress = 0;
int Waddress_space = 1;
int Wbitwise = 1;
int Wcast_from_as = 0;
int Wcast_to_as = 0;
int Wcast_truncate = 1;
int Wconstexpr_not_const = 0;
@@ -678,6 +679,7 @@ static const struct flag warnings[] = {
{ "address", &Waddress },
{ "address-space", &Waddress_space },
{ "bitwise", &Wbitwise },
{ "cast-from-as", &Wcast_from_as },
{ "cast-to-as", &Wcast_to_as },
{ "cast-truncate", &Wcast_truncate },
{ "constexpr-not-const", &Wconstexpr_not_const},
1 lib.h
@@ -137,6 +137,7 @@ extern int preprocess_only;
extern int Waddress;
extern int Waddress_space;
extern int Wbitwise;
extern int Wcast_from_as;
extern int Wcast_to_as;
extern int Wcast_truncate;
extern int Wconstexpr_not_const;
@@ -77,6 +77,15 @@ Sparse issues these warnings by default. To turn them off, use
\fB\-Wno\-bitwise\fR.
.
.TP
.B \-Wcast\-from\-as
Warn about which remove an address space to a pointer type.

This is similar to \fB\-Waddress\-space\fR but will also warn
on casts to \fBunsigned long\fR.

Sparse does not issues these warnings by default.
.
.TP
.B \-Wcast\-to\-as
Warn about casts which add an address space to a pointer type.

@@ -0,0 +1,56 @@
#define __user __attribute__((address_space(1)))

typedef unsigned long ulong;
typedef long long llong;
typedef struct s obj_t;

static void expl(int i, ulong u, llong l, void *v, obj_t *o, obj_t __user *p)
{
(obj_t*)(i);
(obj_t __user*)(i);

(obj_t*)(u);
(obj_t __user*)(u);

(obj_t*)(l);
(obj_t __user*)(l);

(obj_t*)(v);
(obj_t __user*)(v);

(int)(o);
(ulong)(o);
(llong)(o);
(void *)(o);
(obj_t*)(o);
(obj_t __user*)(o);

(int)(p); // w
(ulong)(p); // w!
(llong)(p); // w
(void *)(p); // w
(obj_t*)(p); // w
(obj_t __user*)(p); // ok
}

/*
* check-name: Waddress-space-strict
* check-command: sparse -Wcast-from-as -Wcast-to-as $file
*
* check-error-start
Waddress-space-strict.c:10:10: warning: cast adds address space to expression (<asn:1>)
Waddress-space-strict.c:13:10: warning: cast adds address space to expression (<asn:1>)
Waddress-space-strict.c:16:10: warning: cast adds address space to expression (<asn:1>)
Waddress-space-strict.c:19:10: warning: cast adds address space to expression (<asn:1>)
Waddress-space-strict.c:26:10: warning: cast adds address space to expression (<asn:1>)
Waddress-space-strict.c:28:10: warning: cast removes address space of expression
Waddress-space-strict.c:29:10: warning: cast removes address space of expression
Waddress-space-strict.c:30:10: warning: cast removes address space of expression
Waddress-space-strict.c:31:10: warning: cast removes address space of expression
Waddress-space-strict.c:32:10: warning: cast removes address space of expression
Waddress-space-strict.c:9:10: warning: non size-preserving integer to pointer cast
Waddress-space-strict.c:10:10: warning: non size-preserving integer to pointer cast
Waddress-space-strict.c:21:10: warning: non size-preserving pointer to integer cast
Waddress-space-strict.c:28:10: warning: non size-preserving pointer to integer cast
* check-error-end
*/

0 comments on commit 5f960cb

Please sign in to comment.
You can’t perform that action at this time.