This macro serves to cast const away without risking unwanted conversions due to mistakes in the cast. It's similar to C++'s const_cast(), but it is more restrictive: it can only cast away 'const', and not arbitrary qualifiers. David made it a bit more robust. Link: https://lore.kernel.org/linux-hardening/20261111102535.434c2eee@pumpkin/ Co-developed-by: David Laight Signed-off-by: Alejandro Colomar --- include/linux/stddef.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/linux/stddef.h b/include/linux/stddef.h index e1851c50c89b..5201d71db387 100644 --- a/include/linux/stddef.h +++ b/include/linux/stddef.h @@ -194,4 +194,16 @@ enum { #define TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS) \ __TRAILING_OVERLAP(TYPE, NAME, FAM, /* no attrs */, MEMBERS) +/** + * const_cast() - At most, drop const in the left-most derivation of a pointer + * + * @T: Type for the cast. + * @p: Pointer to be casted. + * + * If @p is of type const @T, then it is casted to @T. + * If @p is of type @T, then it is returned without any casts. + * This avoids mistakes that change anything other than 'const' in a cast. + */ +#define const_cast(T, p) _Generic(p, const T: (T) (p), T: (p)) + #endif -- 2.55.0