]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Use LFIND() instead of open-coded search loops
authorAlejandro Colomar <alx@kernel.org>
Thu, 14 Nov 2024 14:27:16 +0000 (15:27 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Jan 2025 13:58:13 +0000 (07:58 -0600)
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/addgrps.c
src/newgrp.c

index 8a1667c1e507b19cf73edabadeb20254cb8c7bde..649b801edd80b737b64d689eced1cedfc3c913e3 100644 (file)
@@ -1,11 +1,10 @@
-/*
- * SPDX-FileCopyrightText: 1989 - 1994, Julianne Frances Haugh
- * SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
- * SPDX-FileCopyrightText: 2001 - 2006, Tomasz Kłoczko
- * SPDX-FileCopyrightText: 2007 - 2009, Nicolas François
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
+// SPDX-FileCopyrightText: 1989-1994, Julianne Frances Haugh
+// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
+// SPDX-FileCopyrightText: 2001-2006, Tomasz Kłoczko
+// SPDX-FileCopyrightText: 2007-2009, Nicolas François
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
 
 #include <config.h>
 
 
 #include "alloc/malloc.h"
 #include "alloc/reallocf.h"
+#include "search/l/lfind.h"
 #include "shadowlog.h"
 
 #ident "$Id$"
 
+
 /*
  * Add groups with names from LIST (separated by commas or colons)
  * to the supplementary group set.  Silently ignore groups which are
@@ -61,7 +62,6 @@ add_groups(const char *list)
        added = false;
        p = buf;
        while (NULL != (g = strsep(&p, ",:"))) {
-               size_t        i;
                struct group  *grp;
 
                grp = getgrnam(g); /* local, no need for xgetgrnam */
@@ -70,11 +70,8 @@ add_groups(const char *list)
                        continue;
                }
 
-               for (i = 0; i < (size_t)ngroups && grouplist[i] != grp->gr_gid; i++);
-
-               if (i < (size_t)ngroups) {
+               if (LFIND(&grp->gr_gid, grouplist, ngroups) != NULL)
                        continue;
-               }
 
                if (ngroups >= sysconf (_SC_NGROUPS_MAX)) {
                        fputs (_("Warning: too many groups\n"), shadow_logfd);
index 823d2fe0a931aab0329e989bdef934506457336f..7a5fecabec6cc73e35d8a9fd53e6565f577ed8f3 100644 (file)
@@ -1,11 +1,10 @@
-/*
- * SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh
- * SPDX-FileCopyrightText: 1996 - 2000, Marek Michałkiewicz
- * SPDX-FileCopyrightText: 2001 - 2006, Tomasz Kłoczko
- * SPDX-FileCopyrightText: 2007 - 2008, Nicolas François
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
+// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
+// SPDX-FileCopyrightText: 1996-2000, Marek Michałkiewicz
+// SPDX-FileCopyrightText: 2001-2006, Tomasz Kłoczko
+// SPDX-FileCopyrightText: 2007-2008, Nicolas François
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
 
 #include <config.h>
 
@@ -25,6 +24,7 @@
 #include "exitcodes.h"
 #include "getdef.h"
 #include "prototypes.h"
+#include "search/l/lfind.h"
 #include "shadowlog.h"
 #include "string/sprintf/snprintf.h"
 #include "string/strcmp/streq.h"
@@ -628,12 +628,7 @@ fail_gg:
         * database. However getgroups() will return the group. So
         * if she is listed there already it is ok to grant membership.
         */
-       for (int i = 0; i < ngroups; i++) {
-               if (grp->gr_gid == grouplist[i]) {
-                       is_member = true;
-                       break;
-               }
-       }
+       is_member = (LFIND(&grp->gr_gid, grouplist, ngroups) != NULL);
 
        /*
         * For split groups (due to limitations of NIS), check all
@@ -687,13 +682,7 @@ fail_gg:
         */
        grouplist = XREALLOC(grouplist, ngroups + 1, GETGROUPS_T);
 
-       int i;
-       for (i = 0; i < ngroups; i++) {
-               if (gid == grouplist[i]) {
-                       break;
-               }
-       }
-       if (i == ngroups) {
+       if (LFIND(&gid, grouplist, ngroups) == NULL) {
                if (ngroups >= sysconf (_SC_NGROUPS_MAX)) {
                        (void) fputs (_("too many groups\n"), stderr);
                } else {