[PATCH v11] posix: Deprecate group_member for Linux

Paul Eggert eggert@cs.ucla.edu
Wed Mar 27 20:57:37 GMT 2024


On 3/27/24 12:43, Joe Simmons-Talbott wrote:
> +  int granted;
> +
> +  if (uid == stats.st_uid)
> +    granted = (unsigned int) (stats.st_mode & (mode << 6)) >> 6;
> +  else
> +    {
> +      if (((flag & AT_EACCESS) && stats.st_gid == __getegid()) ||
> +          (!(flag & AT_EACCESS) && stats.st_gid == __getgid()))
> +        granted = (unsigned int) (stats.st_mode & (mode << 3)) >> 3;
> +      else
> +        {
> +          int gm = __group_member2 (stats.st_gid);
> +          if (gm == -1)
> +            return -1;
> +          else if (gm)
> +            granted = (unsigned int) (stats.st_mode & (mode << 3)) >> 3;
> +          else
> +            granted = (stats.st_mode & mode);
> +        }
> +    }
>   
>     if (granted == mode)
>       return 0;

This is still too complicated, with repetitions of AT_EACCESS that were 
not in the original. Come to think of it, it has unnecessary casts and 
repetitive shifting and masking, and that indentation of || is not GNU 
style. How about something like the following untested code instead?

   int shift;

   if (uid == stats.st_uid)
     shift = 6;
   else
     {
       int gm = (stats.st_gid
		== (flag & AT_EACCESS ? __getegid () : __getgid ()));
       if (!gm)
	{
           gm = __group_member2 (stats.st_gid);
           if (gm < 0)
             return gm;
         }
       shift = gm ? 3 : 0;
     }

   if ((stats.st_mode >> shift & mode) == mode)
     return 0;



More information about the Libc-alpha mailing list