From patchwork Sun Mar 1 18:14:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 444970 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 E491614009B for ; Mon, 2 Mar 2015 19:42:36 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=P0FBIK2r; 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=Yq/jBrXT4QaDnO+f0JWK+1hpzkNhcX0 d+onuYf8R6QCNJOq+4N7SkzM08WXPnRZq54wsFRIMf3DSSbmzs5PBsG8w6bSDCSf eCKOGCi/j+d76g5p1NonIbhSF7zncAs/GavRvZ84CCw/7NZ21WvpzWfwhQZrsO06 AOXpR2ZmWZ3E= 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=8h07z8t4weXFavrdlKgLwN4gCR8=; b=P0FBI K2rjymuBtZKoYLvNZOkXlsw1FeTwhC/fIo7fFohMlcdWhm4Bnt7NnMxxkhDOH0Mh 0fHxGxN5xm4ez/B3dy/2WRVFzXP1Xrxh3xGm0FvTwnuMsfdg/RwFwFyj7jfG6Clg j9h6eBEwhzigOFDdRO8ZDZZGZ5ohtrWdXs1xn0= Received: (qmail 87861 invoked by alias); 2 Mar 2015 08:42:30 -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 87824 invoked by uid 89); 2 Mar 2015 08:42:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, DATE_IN_PAST_12_24, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Message-Id: <4e6492a58091c899640e5c23662dd00048526939.1425285061.git.fweimer@redhat.com> In-Reply-To: References: From: Florian Weimer Date: Sun, 1 Mar 2015 19:14:29 +0100 Subject: [PATCH 19/25] getlogin_r (Linux variant): Switch to struct scratch_buffer To: libc-alpha@sourceware.org This corrects the alloca accounting as a side effect. It was not off if extend_alloca failed to merge allocations. --- sysdeps/unix/sysv/linux/getlogin_r.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c b/sysdeps/unix/sysv/linux/getlogin_r.c index 2c52b21..7904f47 100644 --- a/sysdeps/unix/sysv/linux/getlogin_r.c +++ b/sysdeps/unix/sysv/linux/getlogin_r.c @@ -18,6 +18,7 @@ #include #include #include +#include #define STATIC static static int getlogin_r_fd0 (char *name, size_t namesize); @@ -56,28 +57,19 @@ __getlogin_r_loginuid (name, namesize) endp == uidbuf || *endp != '\0')) return -1; - size_t buflen = 1024; - char *buf = alloca (buflen); - bool use_malloc = false; struct passwd pwd; struct passwd *tpwd; int result = 0; int res; + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); - while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) == ERANGE) - if (__libc_use_alloca (2 * buflen)) - buf = extend_alloca (buf, buflen, 2 * buflen); - else + while ((res = __getpwuid_r (uid, &pwd, + tmpbuf.data, tmpbuf.length, &tpwd)) == ERANGE) + if (!scratch_buffer_grow (&tmpbuf)) { - buflen *= 2; - char *newp = realloc (use_malloc ? buf : NULL, buflen); - if (newp == NULL) - { - result = ENOMEM; - goto out; - } - buf = newp; - use_malloc = true; + result = ENOMEM; + goto out; } if (res != 0 || tpwd == NULL) @@ -97,9 +89,7 @@ __getlogin_r_loginuid (name, namesize) memcpy (name, pwd.pw_name, needed); out: - if (use_malloc) - free (buf); - + scratch_buffer_free (&tmpbuf); return result; }