From: Alejandro Colomar Date: Fri, 14 Nov 2025 01:29:16 +0000 (+0100) Subject: lib/: Use compound literals to avoid casts X-Git-Tag: 4.19.0-rc1~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9705effba5a0af3d87848bee0a434ff4c2520aef;p=thirdparty%2Fshadow.git lib/: Use compound literals to avoid casts 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 --- diff --git a/lib/alloc/calloc.h b/lib/alloc/calloc.h index 5f26bf64d..f9dc9ff61 100644 --- a/lib/alloc/calloc.h +++ b/lib/alloc/calloc.h @@ -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)) diff --git a/lib/alloc/malloc.h b/lib/alloc/malloc.h index d43f2f995..d9ffc3627 100644 --- a/lib/alloc/malloc.h +++ b/lib/alloc/malloc.h @@ -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)) diff --git a/lib/alloc/realloc.h b/lib/alloc/realloc.h index bbf3c5f95..09599c09f 100644 --- a/lib/alloc/realloc.h +++ b/lib/alloc/realloc.h @@ -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) diff --git a/lib/alloc/reallocf.h b/lib/alloc/reallocf.h index a4b61806d..5854aa9c5 100644 --- a/lib/alloc/reallocf.h +++ b/lib/alloc/reallocf.h @@ -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) diff --git a/lib/search/cmp/cmp.h b/lib/search/cmp/cmp.h index 49443f673..3e81e052e 100644 --- a/lib/search/cmp/cmp.h +++ b/lib/search/cmp/cmp.h @@ -11,7 +11,7 @@ #define CMP(T) \ ( \ - _Generic((T) 0, \ + _Generic((T){}, \ int: cmp_int, \ long: cmp_long, \ unsigned int: cmp_uint, \ diff --git a/lib/search/l/lfind.h b/lib/search/l/lfind.h index 06ab775fb..7bbd16e0e 100644 --- a/lib/search/l/lfind.h +++ b/lib/search/l/lfind.h @@ -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)) diff --git a/lib/search/l/lsearch.h b/lib/search/l/lsearch.h index 80d548165..421c4346a 100644 --- a/lib/search/l/lsearch.h +++ b/lib/search/l/lsearch.h @@ -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)) diff --git a/lib/sizeof.h b/lib/sizeof.h index 469bfde5f..0b82ac977 100644 --- a/lib/sizeof.h +++ b/lib/sizeof.h @@ -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)