lib/pwdutils: use assert to check correct usage.

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 <erico.erc@gmail.com>
This commit is contained in:
Érico Nogueira 2021-07-27 23:58:26 -03:00 committed by Karel Zak
parent 8ab700b933
commit 692167ff5e
1 changed files with 6 additions and 6 deletions

View File

@ -3,6 +3,7 @@
* it what you wish. * it what you wish.
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include "c.h" #include "c.h"
#include "pwdutils.h" #include "pwdutils.h"
@ -17,8 +18,8 @@ struct passwd *xgetpwnam(const char *username, char **pwdbuf)
struct passwd *pwd = NULL, *res = NULL; struct passwd *pwd = NULL, *res = NULL;
int rc; int rc;
if (!pwdbuf || !username) assert(pwdbuf);
return NULL; assert(username);
*pwdbuf = xmalloc(UL_GETPW_BUFSIZ); *pwdbuf = xmalloc(UL_GETPW_BUFSIZ);
pwd = xcalloc(1, sizeof(struct passwd)); pwd = xcalloc(1, sizeof(struct passwd));
@ -49,8 +50,8 @@ struct group *xgetgrnam(const char *groupname, char **grpbuf)
struct group *grp = NULL, *res = NULL; struct group *grp = NULL, *res = NULL;
int rc; int rc;
if (!grpbuf || !groupname) assert(grpbuf);
return NULL; assert(groupname);
*grpbuf = xmalloc(UL_GETPW_BUFSIZ); *grpbuf = xmalloc(UL_GETPW_BUFSIZ);
grp = xcalloc(1, sizeof(struct group)); grp = xcalloc(1, sizeof(struct group));
@ -77,8 +78,7 @@ struct passwd *xgetpwuid(uid_t uid, char **pwdbuf)
struct passwd *pwd = NULL, *res = NULL; struct passwd *pwd = NULL, *res = NULL;
int rc; int rc;
if (!pwdbuf) assert(pwdbuf);
return NULL;
*pwdbuf = xmalloc(UL_GETPW_BUFSIZ); *pwdbuf = xmalloc(UL_GETPW_BUFSIZ);
pwd = xcalloc(1, sizeof(struct passwd)); pwd = xcalloc(1, sizeof(struct passwd));