From patchwork Sat Jan 25 12:19:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martijn Dekker X-Patchwork-Id: 11351555 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 43B5F921 for ; Sat, 25 Jan 2020 12:20:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2A0F1206D6 for ; Sat, 25 Jan 2020 12:20:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729098AbgAYMUE (ORCPT ); Sat, 25 Jan 2020 07:20:04 -0500 Received: from kahlil.inlv.org ([37.59.109.123]:58604 "EHLO kahlil.inlv.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726191AbgAYMUD (ORCPT ); Sat, 25 Jan 2020 07:20:03 -0500 Received: from breedzicht.fritz.box (inlv.demon.nl [82.161.110.186]) (authenticated bits=0) by kahlil.inlv.org (8.15.2/8.15.2) with ESMTPSA id 00PCJuXE030397 (version=TLSv1.2 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Sat, 25 Jan 2020 13:19:56 +0100 Subject: [PATCH] parseheredoc: fix alias expansion: save and restore checkkwd To: Harald van Dijk , DASH shell mailing list References: <40db0aa1-195b-f29e-5068-01fa101affd9@inlv.org> <8b48cd04-b85f-1086-55c8-8e17800f5693@gigawatt.nl> From: Martijn Dekker Message-ID: <0c506ff6-6da5-f117-b0b3-d3cf7cc008ed@inlv.org> Date: Sat, 25 Jan 2020 13:19:56 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: Content-Language: en-GB Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org Op 25-01-20 om 02:38 schreef Harald van Dijk: > On 25/01/2020 01:13, Harald van Dijk wrote: >> On 24/01/2020 23:48, Martijn Dekker wrote: >>> There is a regression involving alias expansion, a here-document, and >>> a command substitution. The following worked fine until dash 0.5.8; >>> it throws a syntax error as of dash 0.5.9. >>> >>> alias BEGIN='{' END='}' >>> BEGIN >>> cat <>> $(echo hi) >>> eof >>> END >> >> Nice find. >> >> When the newline after cat <> indicate that the shell is in a state where aliases can be expanded. >> Then, parseheredoc() is called, which in turn calls readtoken1() to >> parse the here-document. readtoken() re-sets checkkwd once it is done, >> but readtoken1() does not, so normally this preserves the "can expand >> aliases" state. However, nested command substitutions do reset >> checkkwd, so things break. >> >> Until 0.5.8, parseheredoc() was called first, and only after that did >> checkkwd get changed. >> >> Either parseheredoc() needs to save and restore checkkwd, or the code >> calling parseheredoc() needs to ensure that it sets checkkwd as >> appropriate afterwards. Saving and restoring checkkwd in parseheredoc() seems the simplest and the most future-proof, so here's a patch to do that. > There is another place that parseheredoc() can be called from where > checkkwd was not being corrected afterwards: > >   alias BEGIN='{' END='}' >   : <   $(echo hi) >   EOF >   BEGIN >   echo ok >   END > > This has been failing for longer. The patch fixes this as well. diff --git a/src/parser.c b/src/parser.c index b318b08..8840262 100644 --- a/src/parser.c +++ b/src/parser.c @@ -661,6 +661,7 @@ parsefname(void) STATIC void parseheredoc(void) { + int savecheckkwd = checkkwd; struct heredoc *here; union node *n; @@ -683,6 +684,8 @@ parseheredoc(void) here->here->nhere.doc = n; here = here->next; } + + checkkwd = savecheckkwd; } STATIC int