lib/cast.h: const_cast(): Add macro for dropping 'const'
Uses of this macro indicate a code smell, but in some cases, libc functions require breaking const correctness. Use this macro to wrap casts in such cases, so that we limit the danger of the cast. It only permits discarding const. Discarding any other qualifiers, or doing other type changes should result in a compile-time error. Link: <https://software.codidact.com/posts/286575/287345#answer-287345> Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
committed by
Iker Pedrosa
parent
4ef08548cc
commit
e9fc8fc7ef
@@ -37,6 +37,7 @@ libshadow_la_SOURCES = \
|
||||
basename.c \
|
||||
bit.c \
|
||||
bit.h \
|
||||
cast.h \
|
||||
chkname.c \
|
||||
chkname.h \
|
||||
chowndir.c \
|
||||
|
||||
21
lib/cast.h
Normal file
21
lib/cast.h
Normal file
@@ -0,0 +1,21 @@
|
||||
// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#ifndef SHADOW_INCLUDE_LIB_CAST_H_
|
||||
#define SHADOW_INCLUDE_LIB_CAST_H_
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include "must_be.h"
|
||||
|
||||
|
||||
#define const_cast(T, p) \
|
||||
({ \
|
||||
static_assert(is_same_type(typeof(&*(p)), const T), ""); \
|
||||
(T) (p); \
|
||||
})
|
||||
|
||||
|
||||
#endif // include guard
|
||||
Reference in New Issue
Block a user