- int n_group_slots;
- struct group_info *gi;
- GETGROUPS_T *group;
-
- /* getgroups () returns the number of elements that it was able to
- place into the array. We simply continue to call getgroups ()
- until the number of elements placed into the array is smaller than
- the physical size of the array. */
-
- group = NULL;
- n_groups = 0;
- n_group_slots = 0;
- while (n_groups == n_group_slots)
- {
- n_group_slots += 64;
- group = (GETGROUPS_T *) xrealloc (group,
- n_group_slots * sizeof (GETGROUPS_T));
- n_groups = getgroups (n_group_slots, group);
- }
+ int n_group_slots = getgroups (0, NULL);
+ gid_t *group;
+
+ if (n_group_slots < 0)
+ return false;
+
+ /* Avoid xnmalloc, as it goes awry when SIZE_MAX < n_group_slots. */
+ if (xalloc_oversized (n_group_slots, sizeof *group))
+ xalloc_die ();
+ group = xmalloc (n_group_slots * sizeof *group);
+ n_groups = getgroups (n_group_slots, group);