]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/user-util: modernize getgroups_alloc() a bit 35226/head
authorMike Yuan <me@yhndnzj.com>
Mon, 18 Nov 2024 17:57:53 +0000 (18:57 +0100)
committerMike Yuan <me@yhndnzj.com>
Tue, 10 Dec 2024 19:51:14 +0000 (20:51 +0100)
- Make sure ret is initialized if we return >= 0
- Reduce variable scope

src/basic/user-util.c
src/basic/user-util.h
src/test/test-user-util.c

index 80c34b311b82bde9c54d54d489dfbf92edb9912f..e050bd4e7b2b7ca980ab604c9006cf91a0bda476 100644 (file)
@@ -529,20 +529,24 @@ int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t
         return (int)nresult;
 }
 
-int getgroups_alloc(gid_t** gids) {
-        gid_t *allocated;
-        _cleanup_free_  gid_t *p = NULL;
+int getgroups_alloc(gid_t **ret) {
         int ngroups = 8;
-        unsigned attempt = 0;
 
-        allocated = new(gid_t, ngroups);
-        if (!allocated)
-                return -ENOMEM;
-        p = allocated;
+        assert(ret);
+
+        for (unsigned attempt = 0;;) {
+                _cleanup_free_ gid_t *p = NULL;
+
+                p = new(gid_t, ngroups);
+                if (!p)
+                        return -ENOMEM;
 
-        for (;;) {
                 ngroups = getgroups(ngroups, p);
-                if (ngroups >= 0)
+                if (ngroups > 0) {
+                        *ret = TAKE_PTR(p);
+                        return ngroups;
+                }
+                if (ngroups == 0)
                         break;
                 if (errno != EINVAL)
                         return -errno;
@@ -557,17 +561,11 @@ int getgroups_alloc(gid_t** gids) {
                 if (ngroups < 0)
                         return -errno;
                 if (ngroups == 0)
-                        return false;
-
-                free(allocated);
-
-                p = allocated = new(gid_t, ngroups);
-                if (!allocated)
-                        return -ENOMEM;
+                        break;
         }
 
-        *gids = TAKE_PTR(p);
-        return ngroups;
+        *ret = NULL;
+        return 0;
 }
 
 int get_home_dir(char **ret) {
index 6f221ebfb0b321fc661c89f2dbe71417ea88833c..653f14254afc5c294fd06b5f58ef1f3349a0f292 100644 (file)
@@ -64,7 +64,7 @@ int in_gid(gid_t gid);
 int in_group(const char *name);
 
 int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **result);
-int getgroups_alloc(gid_t** gids);
+int getgroups_alloc(gid_t **ret);
 
 int get_home_dir(char **ret);
 int get_shell(char **ret);
index af06161bad39ef42369f320587d71467cc11c7d8..bf8dd16d5ba66f5b64534093841bb26c59af074c 100644 (file)
@@ -444,9 +444,8 @@ TEST(gid_lists_ops) {
         assert_se(nresult >= 0);
         assert_se(memcmp_nn(result2, ELEMENTSOF(result2), res4, nresult) == 0);
 
-        nresult = getgroups_alloc(&gids);
-        assert_se(nresult >= 0 || nresult == -EINVAL || nresult == -ENOMEM);
-        assert_se(gids);
+        ASSERT_OK(nresult = getgroups_alloc(&gids));
+        assert_se(gids || nresult == 0);
 }
 
 TEST(parse_uid_range) {