From 5942cc5117dc5b5f2e8db361f2a1451a0b2454d6 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Wed, 6 Nov 2024 01:35:47 +0100 Subject: [PATCH] lib/gshadow.c: Don't exit(3) on error in library code 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: 4eed3e84a148 ("lib/gshadow.c: Use XREALLOC() instead of silently continuing on ENOMEM") Cc: Serge Hallyn Signed-off-by: Alejandro Colomar --- lib/gshadow.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/gshadow.c b/lib/gshadow.c index bf4490276..d7f855073 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -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" @@ -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, ","); @@ -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; }