From patchwork Mon Jun 25 17:49:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 934501 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-93596-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="BGUXAjH5"; dkim-atps=neutral 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 41DxXz17Dxz9rvt for ; Tue, 26 Jun 2018 03:50:42 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; q=dns; s=default; b= erUoQFJV1EnQwLoqPX4HY9AR+o/aVLH0COYggR1ly6709y7/BhUC4YR66S48zkWW 4LG7aAOPn9ScFBjIGb7zYMJbIXyZIhx4kpjN8vsXgOkL/VEbKy8BVnS7GFEDkeWN 5XrRPV+qRjvBnwpkdFSPqmGz+QbZApQdjPNOAtJ9R5U= 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:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; s=default; bh=yZjgg9 dFIFkgqiWj9cF/M2zZLJo=; b=BGUXAjH5Ue+j2wNcdmVgVnQk8OFf6xPaBY1H6F saBqPftJX41DIFLCYykKIPKTUO1a52/kJhY0SK6frlmah9LGTjfQCYuDUkdq8Kw0 JdEN/60N5/TH2JACVf/VLfx/Rv3pqoCRiDui0r5YSLXA6v6pXPX5Mid7uESti3FC uHwao= Received: (qmail 95394 invoked by alias); 25 Jun 2018 17:49:58 -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 95177 invoked by uid 89); 25 Jun 2018 17:49:57 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=inh X-HELO: mx1.redhat.com Date: Mon, 25 Jun 2018 19:49:53 +0200 To: libc-alpha@sourceware.org Subject: [PATCH] getent: Use dynarray in initgroups_keys [BZ #18023] User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20180625174953.F1C8943994575@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) 2018-06-25 Florian Weimer [BZ #18023] * nss/getent.c (initgroups_keys): Use dynarray instead of extend_alloca. diff --git a/nss/getent.c b/nss/getent.c index e609e5f9bb..92ade41d75 100644 --- a/nss/getent.c +++ b/nss/getent.c @@ -39,6 +39,7 @@ #include #include #include +#include /* Get libc version number. */ #include @@ -473,34 +474,51 @@ netgroup_keys (int number, char *key[]) return result; } +#define DYNARRAY_STRUCT gid_list +#define DYNARRAY_ELEMENT gid_t +#define DYNARRAY_PREFIX gid_list_ +#define DYNARRAY_INITIAL_SIZE 10 +#include + /* This is for initgroups */ 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 gid_list list; + gid_list_init (&list); + if (!gid_list_resize (&list, 10)) + { + fprintf (stderr, _("Could not allocate group list: %m\n")); + return 3; + } + for (int i = 0; i < number; ++i) { - int no = ngrps; + int no = gid_list_size (&list); int n; - while ((n = getgrouplist (key[i], -1, grps, &no)) == -1 - && no > ngrps) + while ((n = getgrouplist (key[i], -1, gid_list_begin (&list), &no)) == -1 + && no > gid_list_size (&list)) { - grps = extend_alloca (grps, grpslen, no * sizeof (gid_t)); - ngrps = no; + if (!gid_list_resize (&list, no)) + { + fprintf (stderr, _("Could not allocate group list: %m\n")); + return 3; + } } if (n == -1) - return 1; + { + gid_list_free (&list); + return 1; + } + const gid_t *grps = gid_list_begin (&list); printf ("%-21s", key[i]); for (int j = 0; j < n; ++j) if (grps[j] != -1) @@ -508,6 +526,8 @@ initgroups_keys (int number, char *key[]) putchar_unlocked ('\n'); } + gid_list_free (&list); + return 0; }