From patchwork Tue Jun 26 15:46:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 934922 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-93653-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="H+0d+smT"; 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 41FVlc0r7wz9ry1 for ; Wed, 27 Jun 2018 01:46:51 +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= AHqMYnZsBkmO2COOW65MPXJ3Wf9ULsFOTljH1tZDiZAVqggA/6vtihuzCKv2eeTe lpDSh8EmRadiis7N+SnOsq8kjsajw7JojHpnFnw2d8RQp+FEJbCM/2fl3PIbkMGc dyKtRqM+QtcNfl4UAFo5OBRbFGUx9NeDzdDG0/qH9Z8= 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=5IkMA9 2xQTpse1M38BOVwdfvOtU=; b=H+0d+smT2/yKy+WtGDWlM+4aXyoFBXDlqVCsOL A15UObpT+C3fitPfspuclR7JKlG2srGyV0vklZApxEzE8JnUwoRcj4lNOb13bynr wrbeVhr+hhVzNXmMLyf/6afYX2/7MMupG/T4c9uyIscYUhfdedxuXG9IflQVoBXf mxD+0= Received: (qmail 61480 invoked by alias); 26 Jun 2018 15:46:43 -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 61308 invoked by uid 89); 26 Jun 2018 15:46:42 -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, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=HOME X-HELO: mx1.redhat.com Date: Tue, 26 Jun 2018 17:46:38 +0200 To: libc-alpha@sourceware.org Subject: [PATCH] wordexp: Rewrite parse_tilde to use struct scratch_buffer [BZ #18023] User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20180626154638.723FE43994575@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) 2018-06-26 Florian Weimer [BZ #18023] * posix/wordexp.c (parse_tilde): Use struct scratch_buffer instead of extend_alloca. diff --git a/posix/wordexp.c b/posix/wordexp.c index 0b669a8f5e..7548e0329f 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> @@ -299,12 +299,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 @@ -319,25 +314,38 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length, } else { - uid = __getuid (); - buffer = __alloca (buflen); + struct passwd pwd, *tpwd; + uid_t uid = __getuid (); + int result; + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); - while ((result = __getpwuid_r (uid, &pwd, buffer, buflen, &tpwd)) != 0 + 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 @@ -345,13 +353,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); @@ -363,6 +373,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;