svn commit: r193832 - projects/ngroups/lib/libc/gen

Bruce Evans brde at optusnet.com.au
Wed Jun 10 09:30:56 UTC 2009


On Tue, 9 Jun 2009, Brooks Davis wrote:

> Log:
>  Fix some style bugs and return the right error.  Also, document the new
>  out of memory error condition.

> Modified: projects/ngroups/lib/libc/gen/initgroups.c
> ==============================================================================
> --- projects/ngroups/lib/libc/gen/initgroups.c	Tue Jun  9 14:07:29 2009	(r193831)
> +++ projects/ngroups/lib/libc/gen/initgroups.c	Tue Jun  9 14:18:16 2009	(r193832)
> int
> @@ -56,12 +56,11 @@ initgroups(uname, agroup)
> 	 * setgroups to fail and set errno.
> 	 */
> 	ngroups = sysconf(_SC_NGROUPS_MAX) + 1;
> -	groups = malloc(sizeof(gid_t)*ngroups);
> -	if (groups == NULL)
> -		return (ENOSPC);
> +	if ((groups = malloc(sizeof(*groups) * ngroups)) == NULL)
> +		return (ENOMEM);
>
> 	getgrouplist(uname, agroup, groups, &ngroups);
> 	ret = setgroups(ngroups, groups);
> 	free(groups);
> -	return(ret);
> +	return (ret);
> }

BTW, another idea for the allocator function is to use C99 variable length
arrays (VLAs).  In its most hackish form:

#undef NGROUPS
#define	NGROUPS	sysconf(__SC_NGROUPS_MAX)
/* Better use ngroups_max() = above sysconf() with error checking. */

 	gid_t ngroups[NGROUPS + 1];	/* ... No changes to old code .*.

This removes the burden of memory management, including any possibility
of checking for allocation failure and returning ENOMEM.  Running out
of memory would cause bad things but no more than almost any other use
of VLAs, not to mention almost any other use of auto variables and function
calls.

Bruce


More information about the svn-src-projects mailing list