From patchwork Sun Mar 1 18:03:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 444978 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id CE30B1400EA for ; Mon, 2 Mar 2015 19:56:54 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=ejS6O3+y; dkim-adsp=none (unprotected policy); dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:in-reply-to:references:from:date :subject:to; q=dns; s=default; b=Xgrc1EWVS8uq8XjL8BnV13u/YTSPcuW aM52JLy35Jkj3AQJPBXUo6A4+dSHu0giBXXX3G+xZCxEVyiS+QW6fo222JTmbNUy d4NTnz+cDJLb+vWBL56LA1gIoYo3NcczEcOwktA6rTH2m8z7i0bODa3NyBdG+uXK bHEgwwVCmD5M= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:in-reply-to:references:from:date :subject:to; s=default; bh=e+g05tvCxxnv5Z6fmGGclukr/JE=; b=ejS6O 3+yoLz0v45ixwli3k84hu6WXV6/OHbO8/RvAsiBa780QSH3dCKopdBgOeV9nABAt yCtoY2aZgTUh0HIBgXExsO9EDPewX5LYMSw5Pif3alqTCcycKx9B3Pu+sZYSKMrB S6UPi9qaYUPvyfakIGOWqeAwsbfSwHaPV5Uc5g= Received: (qmail 117027 invoked by alias); 2 Mar 2015 08:56:39 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 116901 invoked by uid 89); 2 Mar 2015 08:56:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_40, DATE_IN_PAST_12_24, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Message-Id: <6194d585306c7c1948e4b55d7702fa5122ed19cc.1425285061.git.fweimer@redhat.com> In-Reply-To: References: From: Florian Weimer Date: Sun, 1 Mar 2015 19:03:01 +0100 Subject: [PATCH 16/25] getent: Switch to struct scratch_buffer in initgroups_keys To: libc-alpha@sourceware.org The retry loop is slightly different here because getgrouplist provides size information, so scratch_buffer_set_array_size can be used to grow the buffer in a more precise fashion. --- nss/getent.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/nss/getent.c b/nss/getent.c index 34df848..bae02b3 100644 --- a/nss/getent.c +++ b/nss/getent.c @@ -39,6 +39,7 @@ #include #include #include +#include /* Get libc version number. */ #include @@ -513,30 +514,34 @@ netgroup_keys (int number, char *key[]) static int initgroups_keys (int number, char *key[]) { - int ngrps = 100; - size_t grpslen = ngrps * sizeof (gid_t); - gid_t *grps = alloca (grpslen); - if (number == 0) { fprintf (stderr, _("Enumeration not supported on %s\n"), "initgroups"); return 3; } + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + for (int i = 0; i < number; ++i) { + ssize_t ngrps = tmpbuf.length / sizeof (gid_t); int no = ngrps; int n; - while ((n = getgrouplist (key[i], -1, grps, &no)) == -1 + while ((n = getgrouplist (key[i], -1, tmpbuf.data, &no)) == -1 && no > ngrps) { - grps = extend_alloca (grps, grpslen, no * sizeof (gid_t)); - ngrps = no; + if (!scratch_buffer_set_array_size (&tmpbuf, no, sizeof (gid_t))) + { + fprintf (stderr, _("Could not allocate group list: %m\n")); + return 3; + } } if (n == -1) return 1; + const gid_t *grps = tmpbuf.data; printf ("%-21s", key[i]); for (int j = 0; j < n; ++j) if (grps[j] != -1) @@ -544,6 +549,8 @@ initgroups_keys (int number, char *key[]) putchar_unlocked ('\n'); } + scratch_buffer_free (&tmpbuf); + return 0; }