]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Use compound literals to avoid casts
authorAlejandro Colomar <alx@kernel.org>
Fri, 14 Nov 2025 01:29:16 +0000 (02:29 +0100)
committerSerge Hallyn <serge@hallyn.com>
Sat, 6 Dec 2025 03:22:45 +0000 (21:22 -0600)
Casts are unsafe.

Compound literals also have the ability of converting values, but they
don't have the unwanted effects on safety --casts disable most useful
diagnostics--.

Compound literals are lvalues, which means their address can be taken,
and they can also be assigned to.  To avoid this, we force lvalue
conversion through a statement expression.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/alloc/calloc.h
lib/alloc/malloc.h
lib/alloc/realloc.h
lib/alloc/reallocf.h
lib/search/cmp/cmp.h
lib/search/l/lfind.h
lib/search/l/lsearch.h
lib/sizeof.h

index 5f26bf64d79a9748b7cbab0baaa86cb64b64d024..f9dc9ff6122504a8230a36c760edbdae204d507d 100644 (file)
@@ -16,9 +16,9 @@
 
 #define CALLOC(n, T)   CALLOC_(n, typeas(T))
 #define CALLOC_(n, T)                                                 \
-(                                                                     \
-       (T *) calloc(n, sizeof(T))                                    \
-)
+({                                                                    \
+       (T *){calloc(n, sizeof(T))};                                  \
+})
 
 
 #define XCALLOC(n, T)  exit_if_null(CALLOC(n, T))
index d43f2f99525f4de8daad805de713589ef9603d70..d9ffc36271bb1179a26f14b358eaeecd9529f1bb 100644 (file)
@@ -17,9 +17,9 @@
 
 #define MALLOC(n, T)   MALLOC_(n, typeas(T))
 #define MALLOC_(n, T)                                                 \
-(                                                                     \
-       (T *) mallocarray(n, sizeof(T))                               \
-)
+({                                                                    \
+       (T *){mallocarray(n, sizeof(T))};                             \
+})
 
 
 #define XMALLOC(n, T)  exit_if_null(MALLOC(n, T))
index bbf3c5f95b544094151ad92738931cc1168995e2..09599c09f621d3579d1f5ff3dc7bb5285502f46a 100644 (file)
@@ -16,9 +16,9 @@
 
 #define REALLOC(p, n, T)   REALLOC_(p, n, typeas(T))
 #define REALLOC_(p, n, T)                                             \
-(                                                                     \
-       _Generic(p, T *: (T *) reallocarray_(p, n, sizeof(T)))        \
-)
+({                                                                    \
+       _Generic(p, T *: (T *){reallocarray_(p, n, sizeof(T))});      \
+})
 
 #define reallocarray_(p, n, size)  reallocarray(p, (n) ?: 1, (size) ?: 1)
 
index a4b61806d38afd1db326f807c0ec2b5fc2d4ebe3..5854aa9c521e44ae81ac05b14ca65be77bc846cd 100644 (file)
@@ -17,9 +17,9 @@
 
 #define REALLOCF(p, n, T)   REALLOCF_(p, n, typeas(T))
 #define REALLOCF_(p, n, T)                                            \
-(                                                                     \
-       _Generic(p, T *: (T *) reallocarrayf_(p, n, sizeof(T)))       \
-)
+({                                                                    \
+       _Generic(p, T *: (T *){reallocarrayf_(p, n, sizeof(T))});     \
+})
 
 #define reallocarrayf_(p, n, size)  reallocarrayf(p, (n) ?: 1, (size) ?: 1)
 
index 49443f6738ec93b8755b9c6cafec36b79912e7e8..3e81e052efc12740eaf9626b0530b74aac5bc8d8 100644 (file)
@@ -11,7 +11,7 @@
 
 #define CMP(T)                                                        \
 (                                                                     \
-       _Generic((T) 0,                                               \
+       _Generic((T){},                                               \
                int:            cmp_int,                              \
                long:           cmp_long,                             \
                unsigned int:   cmp_uint,                             \
index 06ab775fb25710c5ea0d126e6fad3afbafbe1dd2..7bbd16e0e3cabc4d53c5f668ed714a04de27b4d4 100644 (file)
@@ -21,7 +21,7 @@
 ({                                                                    \
        _Generic(k, T *: (void)0, const T *: (void)0);                \
        _Generic(a, T *: (void)0, const T *: (void)0);                \
-       (T *) lfind_(k, a, n, sizeof(T), cmp);                        \
+       (T *){lfind_(k, a, n, sizeof(T), cmp)};                       \
 })
 
 #define LFIND(T, ...)  lfind_T(T, __VA_ARGS__, CMP(T))
index 80d5481656eb3e2ca5f758770f1bbbc053270fb7..421c4346afe45baec1485f81143a8503443b98ab 100644 (file)
@@ -20,7 +20,7 @@
 ({                                                                    \
        _Generic(k, T *: (void)0, const T *: (void)0);                \
        _Generic(a, T *: (void)0);                                    \
-       (T *) lsearch(k, a, n, sizeof(T), cmp);                       \
+       (T *){lsearch(k, a, n, sizeof(T), cmp)};                      \
 })
 
 #define LSEARCH(T, ...)  lsearch_T(T, __VA_ARGS__, CMP(T))
index 469bfde5ff20d057f87ac50d62a18106b5329ce3..0b82ac9776b45ec9bba7d14ee3d8b325ff6ce985 100644 (file)
@@ -17,7 +17,7 @@
 
 #define typeas(T)            typeof((T){})
 
-#define ssizeof(x)           ((ssize_t) sizeof(x))
+#define ssizeof(x)           ({(ssize_t){sizeof(x)};})
 #define memberof(T, member)  ((T){}.member)
 #define WIDTHOF(x)           (sizeof(x) * CHAR_BIT)