]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/search/, lib/, src/: Add a type parameter to the type-safe macros
authorAlejandro Colomar <alx@kernel.org>
Fri, 14 Nov 2025 00:20:41 +0000 (01:20 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 5 Dec 2025 14:34:32 +0000 (08:34 -0600)
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 <alx@kernel.org>
lib/addgrps.c
lib/adds.h
lib/search/l/lfind.h
lib/search/l/lsearch.h
lib/search/sort/qsort.h
src/newgrp.c

index b79203205231d6e7f52425efee7f0e91983a2db8..eda3878f40294e4762819fee068a8dd34625e84b 100644 (file)
@@ -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);
 
index 9f551b4e4b121bc074c4372f522e95190650d759..a79a6f8fe3a11c935dfa67818bbd4b39a90787d4 100644 (file)
@@ -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]);
index 1269f9c34e2a2736151313138b8d0956bf3660c3..eb102dc55f53e71e9dbb27854681179dbaded3fd 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
 #include <stddef.h>
 
 #include "search/cmp/cmp.h"
-#include "typetraits.h"
 
-#include <assert.h>
 
-
-#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 *));                   \
 })
 
 
index 60689a7f7c1b1ce99730a055cf5b952e90d0df4a..6f00f3bb12cc5204e578a3947327f836f7d3d4b2 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
 #include <search.h>
 
 #include "search/cmp/cmp.h"
-#include "typetraits.h"
 
-#include <assert.h>
 
-
-#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 *));                  \
 })
 
 
index 78fc73cf4642f90105ad87f3fb8b217de2cc8fc7..f032414576a86f4a32a3f7a637a933e562d649ea 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
 #include <stdlib.h>
 
 #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)
 
 
index 325a9141b00dfb18408c797c53f0bb99a6c79d17..3bf7291a1246d9269c1f3cf793cb9575000a62ad 100644 (file)
@@ -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");