Skip to content

Commit 20e5cc2

Browse files
committed
compiler.h: Introduce __must_be_byte_array()
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 <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent cb7380d commit 20e5cc2

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

include/linux/compiler.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
221221
#endif /* __CHECKER__ */
222222

223223
/* &a[0] degrades to a pointer: a different type from an array */
224-
#define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")
224+
#define __is_array(a) (!__same_type((a), &(a)[0]))
225+
#define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
226+
"must be array")
227+
228+
#define __is_byte_array(a) (__is_array(a) && sizeof((a)[0]) == 1)
229+
#define __must_be_byte_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_byte_array(a), \
230+
"must be byte array")
225231

226232
/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
227233
#define __must_be_cstr(p) \

0 commit comments

Comments
 (0)