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
parent 7f22751bd1
commit 0a002b6179
1 changed files with 6 additions and 6 deletions

View File

@ -3,6 +3,7 @@
* it what you wish.
*/
#include <stdlib.h>
#include <assert.h>
#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));