Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/: sget*ent(): Simplify by calling strdup(3) #1146

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "alloc/malloc.h"
Expand Down Expand Up @@ -69,34 +70,27 @@ void endsgent (void)
}

/*@observer@*//*@null@*/struct sgrp *
sgetsgent(const char *string)
sgetsgent(const char *s)
{
static char *sgrbuf = NULL;
static size_t sgrbuflen = 0;
static char *dup = NULL;

char *fields[FIELDS];
char *cp;
int i;
size_t len = strlen (string) + 1;

if (len > sgrbuflen) {
char *buf = REALLOC(sgrbuf, len, char);
if (NULL == buf)
return NULL;

sgrbuf = buf;
sgrbuflen = len;
}
free(dup);
dup = strdup(s);
if (dup == NULL)
return NULL;

strcpy (sgrbuf, string);
stpsep(sgrbuf, "\n");
stpsep(dup, "\n");

/*
* There should be exactly 4 colon separated fields. Find
* all 4 of them and save the starting addresses in fields[].
*/

for (cp = sgrbuf, i = 0; (i < FIELDS) && (NULL != cp); i++)
for (cp = dup, i = 0; (i < FIELDS) && (NULL != cp); i++)
fields[i] = strsep(&cp, ":");

/*
Expand Down
32 changes: 13 additions & 19 deletions lib/sgetgrent.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

#ident "$Id$"

#include <stdio.h>
#include <sys/types.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "alloc/malloc.h"
#include "alloc/reallocf.h"
Expand Down Expand Up @@ -64,30 +65,23 @@ list(char *s)
}


struct group *sgetgrent (const char *buf)
struct group *
sgetgrent(const char *s)
{
static char *grpbuf = NULL;
static size_t size = 0;
static char *dup = NULL;
static char *grpfields[NFIELDS];
static struct group grent;
int i;
char *cp;

if (strlen (buf) + 1 > size) {
/* no need to use realloc() here - just free it and
allocate a larger block */
free (grpbuf);
size = strlen (buf) + 1000; /* at least: strlen(buf) + 1 */
grpbuf = MALLOC(size, char);
if (grpbuf == NULL) {
size = 0;
return NULL;
}
}
strcpy (grpbuf, buf);
stpsep(grpbuf, "\n");
free(dup);
dup = strdup(s);
if (dup == NULL)
return NULL;

stpsep(dup, "\n");

for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++)
for (cp = dup, i = 0; (i < NFIELDS) && (NULL != cp); i++)
grpfields[i] = strsep(&cp, ":");

if (i < NFIELDS || streq(grpfields[2], "") || cp != NULL) {
Expand Down
28 changes: 12 additions & 16 deletions lib/sgetpwent.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@

#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"
#include "prototypes.h"
#include "shadowlog_internal.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"


#define NFIELDS 7
Expand All @@ -38,33 +40,27 @@
* compilation glarp to improve on this in the future.
*/
struct passwd *
sgetpwent(const char *buf)
sgetpwent(const char *s)
{
static char *dup = 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.
*/
free(dup);
dup = strdup(s);
if (dup == NULL)
return NULL;

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);
stpsep(dup, "\n");

/*
* Save a pointer to the start of each colon separated
* field. The fields are converted into NUL terminated strings.
*/

for (cp = pwdbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++)
for (cp = dup, i = 0; (i < NFIELDS) && (NULL != cp); i++)
fields[i] = strsep(&cp, ":");

/* something at the end, columns over shot */
Expand Down
24 changes: 9 additions & 15 deletions lib/sgetspent.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>

Expand All @@ -36,34 +37,27 @@
* 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 *dup = 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(dup);
dup = strdup(s);
if (dup == 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");
stpsep(dup, "\n");

/*
* Tokenize the string into colon separated fields. Allow up to
* FIELDS different fields.
*/

for (cp = spwbuf, i = 0; cp != NULL && i < FIELDS; i++)
for (cp = dup, i = 0; cp != NULL && i < FIELDS; i++)
fields[i] = strsep(&cp, ":");

if (i == (FIELDS - 1))
Expand Down
Loading