]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Remove arbitrary limitation by calling strdup(3)
authorAlejandro Colomar <alx@kernel.org>
Sat, 7 Dec 2024 21:01:08 +0000 (22:01 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 30 May 2025 21:11:16 +0000 (16:11 -0500)
This makes these APIs more consistent with the ones for groups,
which strdup(3) memory as necessary.

It also makes the code simpler.

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

index b13d5bc5465910d49b0932ec6a4bb409b9dc7124..e155ed0a0ffc72cdc387cb0b1f81b2c509ceb083 100644 (file)
 
 #ident "$Id$"
 
-#include <sys/types.h>
-#include <stdio.h>
 #include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
 
 #include "atoi/getnum.h"
 #include "defines.h"
  *     compilation glarp to improve on this in the future.
  */
 struct passwd *
-sgetpwent(const char *buf)
+sgetpwent(const char *s)
 {
+       static char  *pwdbuf = NULL;
        static struct passwd pwent;
-       static char pwdbuf[PASSWD_ENTRY_MAX_LENGTH];
        int i;
        char *cp;
        char *fields[NFIELDS];
 
-       /*
-        * Copy the string to a static buffer so the pointers into
-        * the password structure remain valid.
-        */
-
-       if (strlen (buf) >= sizeof pwdbuf) {
-               fprintf (shadow_logfd,
-                        "%s: Too long passwd entry encountered, file corruption?\n",
-                        shadow_progname);
-               return NULL;    /* fail if too long */
-       }
-       strcpy (pwdbuf, buf);
+       free(pwdbuf);
+       pwdbuf = strdup(s);
+       if (pwdbuf == NULL)
+               return NULL;
 
        /*
         * Save a pointer to the start of each colon separated
index db85cd024287991dd548a75495393b472cb4a660..8d58f520432dfc014c06fd37848d40dee3a61755 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <stddef.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <string.h>
 
  * sgetspent - convert string in shadow file format to (struct spwd *)
  */
 struct spwd *
-sgetspent(const char *string)
+sgetspent(const char *s)
 {
-       static char spwbuf[PASSWD_ENTRY_MAX_LENGTH];
+       static char  *spwbuf = NULL;
        static struct spwd spwd;
        char *fields[FIELDS];
        char *cp;
        int i;
 
-       /*
-        * Copy string to local buffer.  It has to be tokenized and we
-        * have to do that to our private copy.
-        */
+       free(spwbuf);
+       spwbuf = strdup(s);
+       if (spwbuf == NULL)
+               return NULL;
 
-       if (strlen (string) >= sizeof spwbuf) {
-               fprintf (shadow_logfd,
-                        "%s: Too long passwd entry encountered, file corruption?\n",
-                        shadow_progname);
-               return NULL;    /* fail if too long */
-       }
-       strcpy (spwbuf, string);
        stpsep(spwbuf, "\n");
 
        /*