Skip to contentSkip to conversation

validation: add check for typeof() and address spaces

PATCH Applied Dan Carpenter 2 participants 2 messages Oct 28, 2025 – Dec 1, 2025 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 validation: add check for typeof() and address spaces

The typeof() key word should preserve the address space and typeof_unqual() should not. Add a check to verify this works as expected.

---
 validation/typeof-as.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 validation/typeof-as.c
validation/typeof-as.c new file +32 -0
new file mode 100644
index 000000000000..563685281d92
@@ -0,0 +1,32 @@
+#define __seg_gs		__attribute__((address_space(__seg_gs)))
+static int __seg_gs m;
+
+static int __seg_gs bad_manual (void)
+{
+	return (*(int *)&m);
+}
+
+static int __seg_gs good_manual (void)
+{
+	return (*(int __seg_gs *)&m);
+}
+
+static int bad_typeof (void)
+{
+	return (*(typeof_unqual(m) *)&m);
+}
+
+static int __seg_gs good_typeof (void)
+{
+	return (*(volatile typeof(m) *)&m);
+}
+
+/*
+ * check-name: typeof address space
+ * check-command: ./sparse typeof-as.c
+ *
+ * check-error-start
+typeof-as.c:6:19: warning: cast removes address space '__seg_gs' of expression
+typeof-as.c:16:19: warning: cast removes address space '__seg_gs' of expression
+ * check-error-end
+ */
Signature · 1 line
2.51.0
maintainer Applied
#1 lore raw
Quoted 6 lines · Dan Carpenter

The typeof() key word should preserve the address space and typeof_unqual() should not. Add a check to verify this works as expected.

Applied to sparse-dev.

Chris

Quoted 46 lines
---
 validation/typeof-as.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 validation/typeof-as.c
validation/typeof-as.c new file +32 -1
new file mode 100644
index 000000000000..563685281d92
@@ -0,0 +1,32 @@
+#define __seg_gs               __attribute__((address_space(__seg_gs)))
+static int __seg_gs m;
+
+static int __seg_gs bad_manual (void)
+{
+       return (*(int *)&m);
+}
+
+static int __seg_gs good_manual (void)
+{
+       return (*(int __seg_gs *)&m);
+}
+
+static int bad_typeof (void)
+{
+       return (*(typeof_unqual(m) *)&m);
+}
+
+static int __seg_gs good_typeof (void)
+{
+       return (*(volatile typeof(m) *)&m);
+}
+
+/*
+ * check-name: typeof address space
+ * check-command: ./sparse typeof-as.c
+ *
+ * check-error-start
+typeof-as.c:6:19: warning: cast removes address space '__seg_gs' of expression
+typeof-as.c:16:19: warning: cast removes address space '__seg_gs' of expression
+ * check-error-end
+ */
--

2.51.0