]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Call strchrnul(3) instead of open-coding it
authorAlejandro Colomar <alx@kernel.org>
Mon, 15 Jan 2024 15:20:03 +0000 (16:20 +0100)
committerSerge Hallyn <serge@hallyn.com>
Thu, 14 Mar 2024 22:11:36 +0000 (17:11 -0500)
Performance tests made in 2007 are obsolete.  We should assume libc is
reasonably fast today (otherwise, report a bug to libc).

$ git blame -- lib/sgetgrent.c | grep strchr
45c6603cc (nekral-guest      2007-10-07 11:44:02 +0000  30)  * WARNING: I profiled this once with and without strchr() calls
6f88bcf58 (nekral-guest      2008-05-26 08:31:14 +0000  97)  cp = strchr (cp, ':');

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/sgetgrent.c
lib/sgetpwent.c
lib/sgetspent.c
lib/subordinateio.c

index 6894baf994814e16c5b8bca0ae2b8b2e952ae625..24764ecdd8886076b4d28670ebb37b04a2df9de5 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <grp.h>
+#include <string.h>
 
 #include "alloc.h"
 #include "defines.h"
  *     list() converts the comma-separated list of member names into
  *     an array of character pointers.
  *
- *     WARNING: I profiled this once with and without strchr() calls
- *     and found that using a register variable and an explicit loop
- *     works best.  For large /etc/group files, this is a major win.
- *
  * FINALLY added dynamic allocation.  Still need to fix sgetsgent().
  *  --marekm
  */
-static char **list (char *s)
+static char **
+list(char *s)
 {
        static char **members = NULL;
        static size_t size = 0; /* max members + 1 */
@@ -55,9 +53,7 @@ static char **list (char *s)
                if (!s || s[0] == '\0')
                        break;
                members[i++] = s;
-               while (('\0' != *s) && (',' != *s)) {
-                       s++;
-               }
+               s = strchrnul(s, ',');
                if ('\0' != *s) {
                        *s++ = '\0';
                }
index 8dba02f829a0a9776e051af5a52f49f39e680245..d60a7fe1919f172fe7e7b1f62293a26463855373 100644 (file)
 #ident "$Id$"
 
 #include <sys/types.h>
-#include "defines.h"
 #include <stdio.h>
 #include <pwd.h>
+#include <string.h>
+
+#include "defines.h"
 #include "prototypes.h"
 #include "shadowlog_internal.h"
 
@@ -32,7 +34,8 @@
  *     performance reasons.  I am going to come up with some conditional
  *     compilation glarp to improve on this in the future.
  */
-struct passwd *sgetpwent (const char *buf)
+struct passwd *
+sgetpwent(const char *buf)
 {
        static struct passwd pwent;
        static char pwdbuf[PASSWD_ENTRY_MAX_LENGTH];
@@ -60,9 +63,7 @@ struct passwd *sgetpwent (const char *buf)
 
        for (cp = pwdbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) {
                fields[i] = cp;
-               while (('\0' != *cp) && (':' != *cp)) {
-                       cp++;
-               }
+               cp = strchrnul(cp, ':');
 
                if ('\0' != *cp) {
                        *cp = '\0';
index 184fbc16d91dbd1dff5cd158f25488d86c2d67ee..07104924bee6178080376955a99a74eb4fb8af8b 100644 (file)
 
 #ident "$Id$"
 
+#include <stdio.h>
 #include <sys/types.h>
+#include <string.h>
+
 #include "prototypes.h"
 #include "shadowlog_internal.h"
 #include "defines.h"
-#include <stdio.h>
+
+
 #define        FIELDS  9
 #define        OFIELDS 5
+
+
 /*
  * sgetspent - convert string in shadow file format to (struct spwd *)
  */
-struct spwd *sgetspent (const char *string)
+struct spwd *
+sgetspent(const char *string)
 {
        static char spwbuf[PASSWD_ENTRY_MAX_LENGTH];
        static struct spwd spwd;
@@ -57,9 +64,7 @@ struct spwd *sgetspent (const char *string)
 
        for (cp = spwbuf, i = 0; ('\0' != *cp) && (i < FIELDS); i++) {
                fields[i] = cp;
-               while (('\0' != *cp) && (':' != *cp)) {
-                       cp++;
-               }
+               cp = strchrnul(cp, ':');
 
                if ('\0' != *cp) {
                        *cp = '\0';
index 844de5f063519598b81908f7fbf2286c30afda9f..85bc0adc58e0e1a7f2507a8e7427086d960fc705 100644 (file)
@@ -16,6 +16,7 @@
 #include <pwd.h>
 #include <ctype.h>
 #include <fcntl.h>
+#include <string.h>
 
 #include "alloc.h"
 #include "string/sprintf.h"
@@ -74,7 +75,8 @@ subordinate_free(/*@only@*/void *ent)
  * in @line, or NULL on failure.  Note that the returned value should not
  * be freed by the caller.
  */
-static void *subordinate_parse (const char *line)
+static void *
+subordinate_parse(const char *line)
 {
        static struct subordinate_range range;
        static char rangebuf[1024];
@@ -97,9 +99,7 @@ static void *subordinate_parse (const char *line)
 
        for (cp = rangebuf, i = 0; (i < SUBID_NFIELDS) && (NULL != cp); i++) {
                fields[i] = cp;
-               while (('\0' != *cp) && (':' != *cp)) {
-                       cp++;
-               }
+               cp = strchrnul(cp, ':');
 
                if ('\0' != *cp) {
                        *cp = '\0';