From owner-svn-src-projects@FreeBSD.ORG Wed Jun 10 09:30:56 2009 Return-Path: Delivered-To: svn-src-projects@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 08BA8106564A; Wed, 10 Jun 2009 09:30:56 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from mail02.syd.optusnet.com.au (mail02.syd.optusnet.com.au [211.29.132.183]) by mx1.freebsd.org (Postfix) with ESMTP id 7E1AA8FC14; Wed, 10 Jun 2009 09:30:55 +0000 (UTC) (envelope-from brde@optusnet.com.au) Received: from c122-106-159-184.carlnfd1.nsw.optusnet.com.au (c122-106-159-184.carlnfd1.nsw.optusnet.com.au [122.106.159.184]) by mail02.syd.optusnet.com.au (8.13.1/8.13.1) with ESMTP id n5A9UnA5026894 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 10 Jun 2009 19:30:52 +1000 Date: Wed, 10 Jun 2009 19:30:50 +1000 (EST) From: Bruce Evans X-X-Sender: bde@delplex.bde.org To: Brooks Davis In-Reply-To: <200906091418.n59EIGGD073717@svn.freebsd.org> Message-ID: <20090610191250.A20260@delplex.bde.org> References: <200906091418.n59EIGGD073717@svn.freebsd.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: svn-src-projects@FreeBSD.org, src-committers@FreeBSD.org Subject: Re: svn commit: r193832 - projects/ngroups/lib/libc/gen X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Jun 2009 09:30:56 -0000 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