From d87ecf5dd02e69e978395e125db6380158aae308 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 7 Dec 2024 22:01:08 +0100 Subject: [PATCH] lib/: Remove arbitrary limitation by calling strdup(3) 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 --- lib/sgetpwent.c | 25 +++++++++---------------- lib/sgetspent.c | 20 +++++++------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/sgetpwent.c b/lib/sgetpwent.c index b13d5bc54..e155ed0a0 100644 --- a/lib/sgetpwent.c +++ b/lib/sgetpwent.c @@ -11,10 +11,11 @@ #ident "$Id$" -#include -#include #include +#include +#include #include +#include #include "atoi/getnum.h" #include "defines.h" @@ -38,26 +39,18 @@ * 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 diff --git a/lib/sgetspent.c b/lib/sgetspent.c index db85cd024..8d58f5204 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -36,26 +37,19 @@ * 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"); /* -- 2.47.2