]> Gentwo Git Trees - linux/.git/commitdiff
compiler.h: Introduce __must_be_noncstr()
authorKees Cook <kees@kernel.org>
Thu, 6 Feb 2025 22:54:11 +0000 (14:54 -0800)
committerKees Cook <kees@kernel.org>
Thu, 13 Feb 2025 04:19:26 +0000 (20:19 -0800)
In preparation for adding more type checking to the memtostr/strtomem*()
helpers, introduce the ability to check for the "nonstring" attribute.
This is the reverse of what was added to strscpy*() in commit 559048d156ff
("string: Check for "nonstring" attribute on strscpy() arguments").

Signed-off-by: Kees Cook <kees@kernel.org>
include/linux/compiler.h

index 200fd3c5bc70d0f10e9a9df3c7dc425a063d6507..b4140bb332a37c2ce5e7603e8a3f011e999af4e9 100644 (file)
@@ -206,9 +206,25 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 #define __must_be_byte_array(a)        __BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \
                                                        "must be byte array")
 
+/*
+ * If the "nonstring" attribute isn't available, we have to return true
+ * so the __must_*() checks pass when "nonstring" isn't supported.
+ */
+#if __has_attribute(__nonstring__)
+#define __is_cstr(a)           (!__annotated(a, nonstring))
+#define __is_noncstr(a)                (__annotated(a, nonstring))
+#else
+#define __is_cstr(a)           (true)
+#define __is_noncstr(a)                (true)
+#endif
+
 /* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
 #define __must_be_cstr(p) \
-       __BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)")
+       __BUILD_BUG_ON_ZERO_MSG(!__is_cstr(p), \
+                               "must be C-string (NUL-terminated)")
+#define __must_be_noncstr(p) \
+       __BUILD_BUG_ON_ZERO_MSG(!__is_noncstr(p), \
+                               "must be non-C-string (not NUL-terminated)")
 
 #endif /* __KERNEL__ */