Skip to content

Commit

Permalink
lib/gshadow.c: Don't exit(3) on error in library code
Browse files Browse the repository at this point in the history
We recently replaced bogus REALLOC() calls that didn't correctly handle
errors.  We replaced them by XREALLOC(), which at least would exit and
avoid triggering undefined or unexpected behavior.

Now that we've made the function way simpler, we can handle these errors
by returning NULL, and handling the NULL in the caller.

Fixes: 4eed3e8 ("lib/gshadow.c: Use XREALLOC() instead of silently continuing on ENOMEM")
Cc: Serge Hallyn <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Nov 6, 2024
1 parent fbaeb10 commit 5942cc5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "alloc/malloc.h"
#include "alloc/realloc.h"
#include "alloc/x/xmalloc.h"
#include "defines.h"
#include "prototypes.h"
#include "string/strchr/strchrcnt.h"
Expand All @@ -39,7 +38,9 @@ build_list(char *s)
char **l;
size_t i;

l = XMALLOC(strchrcnt(s, ',') + 2, char *);
l = MALLOC(strchrcnt(s, ',') + 2, char *);
if (l == NULL)
return NULL;

for (i = 0; s != NULL && *s != '\0'; i++)
l[i] = strsep(&s, ",");
Expand Down Expand Up @@ -110,10 +111,14 @@ sgetsgent(const char *string)
sgroup.sg_passwd = fields[1];

free(sgroup.sg_adm);
free(sgroup.sg_mem);

sgroup.sg_adm = build_list(fields[2]);
if (sgroup.sg_adm == NULL)
return NULL;

free(sgroup.sg_mem);
sgroup.sg_mem = build_list(fields[3]);
if (sgroup.sg_mem == NULL)
return NULL;

return &sgroup;
}
Expand Down

0 comments on commit 5942cc5

Please sign in to comment.