From: Thiebaud Weksteen <tweek@xxxxxxxxxx> since it seems that the strict type checking is not done on pointers to restricted types. Signed-off-by: Thiebaud Weksteen <tweek@xxxxxxxxxx> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- validation/bitwise-cast-ptr.c | 34 ++++++++++++++++++++++++++++++++++ validation/bitwise-cast.c | 6 ++++++ 2 files changed, 40 insertions(+) create mode 100644 validation/bitwise-cast-ptr.c diff --git a/validation/bitwise-cast-ptr.c b/validation/bitwise-cast-ptr.c new file mode 100644 index 000000000..c390092ee --- /dev/null +++ b/validation/bitwise-cast-ptr.c @@ -0,0 +1,34 @@ +#define __bitwise __attribute__((bitwise)) +#define __force __attribute__((force)) + +typedef unsigned int u32; +typedef unsigned int __bitwise __be32; + +static __be32* tobi(u32 *x) +{ + return x; // should warn, implicit cast +} + +static __be32* tobe(u32 *x) +{ + return (__be32 *) x; // should warn, explicit cast +} + +static __be32* tobf(u32 *x) +{ + return (__force __be32 *) x; // should not warn, forced cast + return (__be32 __force *) x; // should not warn, forced cast +} + +/* + * check-name: cast of bitwise pointers + * check-command: sparse -Wbitwise $file + * check-known-to-fail + * + * check-error-start +bitwise-cast-ptr.c:9:16: warning: incorrect type in return expression (different base types) +bitwise-cast-ptr.c:9:16: expected restricted __be32 [usertype] * +bitwise-cast-ptr.c:9:16: got unsigned int [usertype] *x +bitwise-cast-ptr.c:14:17: warning: cast to restricted __be32 [usertype] * + * check-error-end + */ diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c index baeca29e7..0583461cb 100644 --- a/validation/bitwise-cast.c +++ b/validation/bitwise-cast.c @@ -29,6 +29,12 @@ static __be32 quux(void) return (__be32)1729; } +/* Explicit case of nonzero forced, legal */ +static __be32 quuy(void) +{ + return (__attribute__((force)) __be32) 1730; +} + /* * check-name: conversions to bitwise types * check-command: sparse -Wbitwise $file -- 2.20.0