From 692167ff5e8c96b4de5673036f72e9be55a38bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Nogueira?= Date: Tue, 27 Jul 2021 23:58:26 -0300 Subject: [PATCH] lib/pwdutils: use assert to check correct usage. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since these functions are only used internally, we can make sure they are being used correctly, and assert() helps in catching remaining issues. Usage of each changed function has been reviewed: For xgetpwnam: - chsh(1) only calls it if a username has been set - login(1) only calls it if username has been set and is not empty - su(1) always initializes new_user to "root" - unshare(1) calls get_user with optarg, so always set as well For xgetgrnam: - unshare(1) calls get_group with optarg For xgetpwuid: - chsh(1) passes a stack allocated struct for struct passwd Signed-off-by: Érico Nogueira --- lib/pwdutils.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/pwdutils.c b/lib/pwdutils.c index 641a9da40..1c1f13e92 100644 --- a/lib/pwdutils.c +++ b/lib/pwdutils.c @@ -3,6 +3,7 @@ * it what you wish. */ #include +#include #include "c.h" #include "pwdutils.h" @@ -17,8 +18,8 @@ struct passwd *xgetpwnam(const char *username, char **pwdbuf) struct passwd *pwd = NULL, *res = NULL; int rc; - if (!pwdbuf || !username) - return NULL; + assert(pwdbuf); + assert(username); *pwdbuf = xmalloc(UL_GETPW_BUFSIZ); pwd = xcalloc(1, sizeof(struct passwd)); @@ -49,8 +50,8 @@ struct group *xgetgrnam(const char *groupname, char **grpbuf) struct group *grp = NULL, *res = NULL; int rc; - if (!grpbuf || !groupname) - return NULL; + assert(grpbuf); + assert(groupname); *grpbuf = xmalloc(UL_GETPW_BUFSIZ); grp = xcalloc(1, sizeof(struct group)); @@ -77,8 +78,7 @@ struct passwd *xgetpwuid(uid_t uid, char **pwdbuf) struct passwd *pwd = NULL, *res = NULL; int rc; - if (!pwdbuf) - return NULL; + assert(pwdbuf); *pwdbuf = xmalloc(UL_GETPW_BUFSIZ); pwd = xcalloc(1, sizeof(struct passwd));