]> www.infradead.org Git - users/hch/xfs.git/commitdiff
compiler.h: Introduce __must_be_byte_array()
authorKees Cook <kees@kernel.org>
Wed, 5 Feb 2025 20:48:07 +0000 (12:48 -0800)
committerKees Cook <kees@kernel.org>
Fri, 7 Feb 2025 02:48:02 +0000 (18:48 -0800)
In preparation for adding stricter type checking to the str/mem*()
helpers, provide a way to check that a variable is a byte array
via __must_be_byte_array().

Suggested-by: Kent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: Kees Cook <kees@kernel.org>
include/linux/compiler.h

index 7af999a131cb23620e2e772b25f94ce03ace31d2..1c0688319435d4d072d51e256ed6b8ef13798548 100644 (file)
@@ -221,7 +221,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
 #endif /* __CHECKER__ */
 
 /* &a[0] degrades to a pointer: a different type from an array */
-#define __must_be_array(a)     __BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")
+#define __is_array(a)          (!__same_type((a), &(a)[0]))
+#define __must_be_array(a)     __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
+                                                       "must be array")
+
+#define __is_byte_array(a)     (__is_array(a) && sizeof((a)[0]) == 1)
+#define __must_be_byte_array(a)        __BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \
+                                                       "must be byte array")
 
 /* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
 #define __must_be_cstr(p) \