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;
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) {
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);
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) {