From: Alejandro Colomar Date: Thu, 14 Nov 2024 14:27:16 +0000 (+0100) Subject: lib/, src/: Use LFIND() instead of open-coded search loops X-Git-Tag: 4.17.3~47 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=85767d1795d1ac2adf5a16113fb2b754122192c0;p=thirdparty%2Fshadow.git lib/, src/: Use LFIND() instead of open-coded search loops Reviewed-by: Serge Hallyn Signed-off-by: Alejandro Colomar --- diff --git a/lib/addgrps.c b/lib/addgrps.c index 8a1667c1e..649b801ed 100644 --- a/lib/addgrps.c +++ b/lib/addgrps.c @@ -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 +// SPDX-License-Identifier: BSD-3-Clause + #include @@ -21,10 +20,12 @@ #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); diff --git a/src/newgrp.c b/src/newgrp.c index 823d2fe0a..7a5fecabe 100644 --- a/src/newgrp.c +++ b/src/newgrp.c @@ -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 +// SPDX-License-Identifier: BSD-3-Clause + #include @@ -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 {