From patchwork Sun Mar 1 18:48:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 444988 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 E6CE41400EA for ; Mon, 2 Mar 2015 20:27:39 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=ly9yRg2u; 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=P3sQaibkWQe/1M8YGLeEEqdqpnutvAt BghlkIgsDRJ5/tJFI14dC7OoZug2IKam7n6sYjcBFpuZvrsjlf0pqHTTCBJ6AexG GsK/q8O4iux6VbYe7TVv9jBbz3JbVLGgF8oTWlwNNbhfG7P2ZsAHbJBQqvv7Rl8v 7bV3WuTZ7/H0= 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=bxrhVRqYzJBvI0+uIyZX2a6AQk4=; b=ly9yR g2uKHTS3VTPDCUd/W6wt+Fi4jFqGWaMCzwKRPeNh3XYI8eIlWkhaAC29az25gFl4 amL2hoGyRwi71fODYoTOQVuXV5YyTznRo6Zc7d6JDZy5+3pclHASEHoZAX04pPbO 1Wmg7W1xG4C0YCOOEYnD3H1vLfVAHVdcR1LYlo= Received: (qmail 115142 invoked by alias); 2 Mar 2015 09:27:33 -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 115118 invoked by uid 89); 2 Mar 2015 09:27:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=AWL, BAYES_50, 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: In-Reply-To: References: From: Florian Weimer Date: Sun, 1 Mar 2015 19:48:31 +0100 Subject: [PATCH 21/25] wordexp: Rewrite parse_tilde to use struct scratch_buffer To: libc-alpha@sourceware.org --- posix/wordexp.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/posix/wordexp.c b/posix/wordexp.c index e3d8d6b..cf5801a 100644 --- a/posix/wordexp.c +++ b/posix/wordexp.c @@ -17,7 +17,6 @@ License along with the GNU C Library; if not, see . */ -#include #include #include #include @@ -41,6 +40,7 @@ #include #include #include +#include #include #include <_itoa.h> @@ -308,12 +308,7 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length, if (i == 1 + *offset) { /* Tilde appears on its own */ - uid_t uid; - struct passwd pwd, *tpwd; - int buflen = 1000; char* home; - char* buffer; - int result; /* POSIX.2 says ~ expands to $HOME and if HOME is unset the results are unspecified. We do a lookup on the uid if @@ -328,25 +323,38 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length, } else { - uid = __getuid (); - buffer = __alloca (buflen); - - while ((result = __getpwuid_r (uid, &pwd, buffer, buflen, &tpwd)) != 0 + struct passwd pwd, *tpwd; + uid_t uid = __getuid (); + int result; + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + + while ((result = __getpwuid_r (uid, &pwd, + tmpbuf.data, tmpbuf.length, + &tpwd)) != 0 && errno == ERANGE) - buffer = extend_alloca (buffer, buflen, buflen + 1000); + if (!scratch_buffer_grow (&tmpbuf)) + return WRDE_NOSPACE; if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL) { *word = w_addstr (*word, word_length, max_length, pwd.pw_dir); if (*word == NULL) - return WRDE_NOSPACE; + { + scratch_buffer_free (&tmpbuf); + return WRDE_NOSPACE; + } } else { *word = w_addchar (*word, word_length, max_length, '~'); if (*word == NULL) - return WRDE_NOSPACE; + { + scratch_buffer_free (&tmpbuf); + return WRDE_NOSPACE; + } } + scratch_buffer_free (&tmpbuf); } } else @@ -354,13 +362,15 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length, /* Look up user name in database to get home directory */ char *user = strndupa (&words[1 + *offset], i - (1 + *offset)); struct passwd pwd, *tpwd; - int buflen = 1000; - char* buffer = __alloca (buflen); int result; + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); - while ((result = __getpwnam_r (user, &pwd, buffer, buflen, &tpwd)) != 0 + while ((result = __getpwnam_r (user, &pwd, tmpbuf.data, tmpbuf.length, + &tpwd)) != 0 && errno == ERANGE) - buffer = extend_alloca (buffer, buflen, buflen + 1000); + if (!scratch_buffer_grow (&tmpbuf)) + return WRDE_NOSPACE; if (result == 0 && tpwd != NULL && pwd.pw_dir) *word = w_addstr (*word, word_length, max_length, pwd.pw_dir); @@ -372,6 +382,8 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length, *word = w_addstr (*word, word_length, max_length, user); } + scratch_buffer_free (&tmpbuf); + *offset = i - 1; } return *word ? 0 : WRDE_NOSPACE;