Date: Wed, 10 Jun 2009 19:30:50 +1000 (EST) From: Bruce Evans <brde@optusnet.com.au> To: Brooks Davis <brooks@FreeBSD.org> Cc: svn-src-projects@FreeBSD.org, src-committers@FreeBSD.org Subject: Re: svn commit: r193832 - projects/ngroups/lib/libc/gen Message-ID: <20090610191250.A20260@delplex.bde.org> In-Reply-To: <200906091418.n59EIGGD073717@svn.freebsd.org> References: <200906091418.n59EIGGD073717@svn.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090610191250.A20260>