]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/pwdutils: use assert to check correct usage.
authorÉrico Nogueira <erico.erc@gmail.com>
Wed, 28 Jul 2021 02:58:26 +0000 (23:58 -0300)
committerÉrico Nogueira <erico.erc@gmail.com>
Thu, 29 Jul 2021 12:58:19 +0000 (09:58 -0300)
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>
lib/pwdutils.c

index 641a9da401150e761669a85be599a08a3e418ce4..1c1f13e9293384f7da6c92fb5095af66886c3d44 100644 (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));