[PATCH v2 1/2] posix/group_member: Add group_member2 with error return.

Florian Weimer fw@deneb.enyo.de
Sat Oct 14 08:52:25 GMT 2023


* Joe Simmons-Talbott:

> index 22422b1f9f..6064eb7264 100644
> --- a/posix/group_member.c
> +++ b/posix/group_member.c
> @@ -16,6 +16,8 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>  
> +#include <errno.h>
> +#include <scratch_buffer.h>
>  #include <sys/types.h>
>  #include <unistd.h>
>  #include <stdlib.h>
> @@ -47,3 +49,33 @@ __group_member (gid_t gid)
>    return 0;
>  }
>  weak_alias (__group_member, group_member)
> +
> +int
> +__group_member2 (gid_t gid)
> +{
> +  int n;
> +  gid_t *groups;
> +  struct scratch_buffer buf;
> +  scratch_buffer_init (&buf);
> +
> +  n = __getgroups (0, NULL);
> +  if (!scratch_buffer_set_array_size (&buf, n, sizeof (*groups)))
> +    {
> +      errno = ENOMEM;
> +      return -1;
> +    }
> +  groups = buf.data;
> +
> +  n = __getgroups (n, groups);
> +
> +  while (n-- > 0)
> +    if (groups[n] == gid)
> +      {
> +        scratch_buffer_free (&buf);
> +        return 1;
> +      }
> +
> +  scratch_buffer_free (&buf);
> +  return 0;
> +}
> +weak_alias (__group_member2, group_member2)

This three-state return value (0/1/-1) is very difficult to deal with
for programmers, especially since we are coming from a boolean
interface.  I'm not sure if this kind of interface is a good idea.


More information about the Libc-alpha mailing list