Skip to content

Commit

Permalink
compiler.h: Introduce __must_be_byte_array()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Kees Cook committed Feb 7, 2025
1 parent cb7380d commit 20e5cc2
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion include/linux/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down

0 comments on commit 20e5cc2

Please sign in to comment.