From f45082a1e94c69eb9dcc4e1844da0f3d987d392f Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 14 Nov 2025 01:20:41 +0100 Subject: [PATCH] lib/search/, lib/, src/: Add a type parameter to the type-safe macros This simplifies the implementation, allowing us to use _Generic(3) to replace _Static_assert(3), is_same_type(), and local variables. Local variables in macros can cause issues when nesting such macros. This also makes the code more readable, by having the type explicitly stated at call site. Signed-off-by: Alejandro Colomar --- lib/addgrps.c | 2 +- lib/adds.h | 2 +- lib/search/l/lfind.h | 16 +++++----------- lib/search/l/lsearch.h | 16 +++++----------- lib/search/sort/qsort.h | 10 ++++------ src/newgrp.c | 4 ++-- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/lib/addgrps.c b/lib/addgrps.c index b79203205..eda3878f4 100644 --- a/lib/addgrps.c +++ b/lib/addgrps.c @@ -61,7 +61,7 @@ add_groups(const char *list) continue; } - LSEARCH(&grp->gr_gid, gids, &n); + LSEARCH(gid_t, &grp->gr_gid, gids, &n); } free(dup); diff --git a/lib/adds.h b/lib/adds.h index 9f551b4e4..a79a6f8fe 100644 --- a/lib/adds.h +++ b/lib/adds.h @@ -55,7 +55,7 @@ addslN(size_t n, long addend[n]) e = errno; while (n > 1) { - QSORT(addend, n); + QSORT(long, addend, n); errno = 0; addend[0] = addsl2(addend[0], addend[--n]); diff --git a/lib/search/l/lfind.h b/lib/search/l/lfind.h index 1269f9c34..eb102dc55 100644 --- a/lib/search/l/lfind.h +++ b/lib/search/l/lfind.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause @@ -12,19 +12,13 @@ #include #include "search/cmp/cmp.h" -#include "typetraits.h" -#include - -#define LFIND(k, a, n) \ +#define LFIND(T, k, a, n) \ ({ \ - __auto_type k_ = k; \ - __auto_type a_ = a; \ - \ - static_assert(is_same_typeof(k_, a_), ""); \ - \ - (typeof(k_)) lfind_(k_, a_, n, sizeof(*k_), CMP(typeof(k_))); \ + _Generic(k, T *: 0, const T *: 0); \ + _Generic(a, T *: 0, const T *: 0); \ + (T *) lfind_(k, a, n, sizeof(T), CMP(T *)); \ }) diff --git a/lib/search/l/lsearch.h b/lib/search/l/lsearch.h index 60689a7f7..6f00f3bb1 100644 --- a/lib/search/l/lsearch.h +++ b/lib/search/l/lsearch.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause @@ -11,19 +11,13 @@ #include #include "search/cmp/cmp.h" -#include "typetraits.h" -#include - -#define LSEARCH(k, a, n) \ +#define LSEARCH(T, k, a, n) \ ({ \ - __auto_type k_ = k; \ - __auto_type a_ = a; \ - \ - static_assert(is_same_typeof(k_, a_), ""); \ - \ - (typeof(k_)) lsearch(k_, a_, n, sizeof(*k_), CMP(typeof(k_)));\ + _Generic(k, T *: 0, const T *: 0); \ + _Generic(a, T *: 0); \ + (T *) lsearch(k, a, n, sizeof(T), CMP(T *)); \ }) diff --git a/lib/search/sort/qsort.h b/lib/search/sort/qsort.h index 78fc73cf4..f03241457 100644 --- a/lib/search/sort/qsort.h +++ b/lib/search/sort/qsort.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar // SPDX-License-Identifier: BSD-3-Clause @@ -11,14 +11,12 @@ #include #include "search/cmp/cmp.h" -#include "typetraits.h" -#define QSORT(a, n) do \ +#define QSORT(T, a, n) do \ { \ - __auto_type p_ = a; \ - \ - qsort(p_, n, sizeof(*p_), CMP(typeof(p_))); \ + _Generic(a, T *: 0); \ + qsort(a, n, sizeof(T), CMP(T *)); \ } while (0) diff --git a/src/newgrp.c b/src/newgrp.c index 325a9141b..3bf7291a1 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -619,7 +619,7 @@ int main (int argc, char **argv) * database. However getgroups() will return the group. So * if she is listed there already it is ok to grant membership. */ - is_member = (LFIND(&grp->gr_gid, gids, ngroups) != NULL); + is_member = (LFIND(gid_t, &grp->gr_gid, gids, ngroups) != NULL); /* * For split groups (due to limitations of NIS), check all @@ -673,7 +673,7 @@ int main (int argc, char **argv) */ gids = XREALLOC(gids, ngroups + 1, gid_t); - LSEARCH(&gid, gids, &ngroups); + LSEARCH(gid_t, &gid, gids, &ngroups); if (setgroups(ngroups, gids) == -1) perror("setgroups"); -- 2.47.3