aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/validation
diff options
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-06-29 00:35:44 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-09-01 22:41:24 +0200
commitd96da358cfa0432f067a4e66940765883b80ee62 (patch)
treee11005607bf311160dbd3ae033abe44be1f1fe20 /validation
parent4fd434fd65a494592fef81937254ba89c9ae6546 (diff)
downloadsparse-dev-d96da358cfa0432f067a4e66940765883b80ee62.tar.gz
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>
Diffstat (limited to 'validation')
-rw-r--r--validation/Waddress-space-strict.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/validation/Waddress-space-strict.c b/validation/Waddress-space-strict.c
new file mode 100644
index 00000000..5071aab2
--- /dev/null
+++ b/validation/Waddress-space-strict.c
@@ -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:18: warning: non size-preserving integer to pointer cast
+Waddress-space-strict.c:10:25: warning: non size-preserving integer to pointer cast
+Waddress-space-strict.c:21:15: warning: non size-preserving pointer to integer cast
+Waddress-space-strict.c:28:15: warning: non size-preserving pointer to integer cast
+ * check-error-end
+ */