From patchwork Tue Apr 28 14:19:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 11514777 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 0395B112C for ; Tue, 28 Apr 2020 14:20:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E05AD206D9 for ; Tue, 28 Apr 2020 14:20:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727026AbgD1OUE (ORCPT ); Tue, 28 Apr 2020 10:20:04 -0400 Received: from helcar.hmeau.com ([216.24.177.18]:47784 "EHLO fornost.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726868AbgD1OUE (ORCPT ); Tue, 28 Apr 2020 10:20:04 -0400 Received: from gwarestrin.me.apana.org.au ([192.168.0.7] helo=gwarestrin.arnor.me.apana.org.au) by fornost.hmeau.com with smtp (Exim 4.89 #2 (Debian)) id 1jTR2k-0003uM-Ve; Wed, 29 Apr 2020 00:17:04 +1000 Received: by gwarestrin.arnor.me.apana.org.au (sSMTP sendmail emulation); Wed, 29 Apr 2020 00:19:59 +1000 Date: Wed, 29 Apr 2020 00:19:59 +1000 From: Herbert Xu To: dash@vger.kernel.org, Harald van Dijk , Martijn Dekker Subject: [PATCH] parser: Fix alias expansion after heredoc or newlines Message-ID: <20200428141958.GA18579@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org The following patch is meant to be applied on top of: https://patchwork.kernel.org/patch/11512439/ ---8<--- This script should print OK: alias a="case x in " b=x a b) echo BAD;; esac alias BEGIN={ END=} BEGIN cat <<- EOF > /dev/null $(:) EOF END : <<- EOF && $(:) EOF BEGIN echo OK END However, because the value of checkkwd is either zeroed when it shouldn't, or isn't zeroed when it should, dash currently gets it wrong in every case. This patch fixes it by saving checkkwd and zeroing it where needed. Suggested-by: Harald van Dijk Reported-by: Harald van Dijk Reported-by: Martijn Dekker Signed-off-by: Herbert Xu diff --git a/src/parser.c b/src/parser.c index 5c9e9a0..be84f8b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -704,10 +704,14 @@ top: if (kwd & CHKNL) { while (t == TNL) { parseheredoc(); + checkkwd = 0; t = xxreadtoken(); } } + kwd |= checkkwd; + checkkwd = 0; + if (t != TWORD || quoteflag) { goto out; } @@ -725,7 +729,7 @@ top: } } - if (checkkwd & CHKALIAS) { + if (kwd & CHKALIAS) { struct alias *ap; if ((ap = lookupalias(wordtext, 1)) != NULL) { if (*ap->val) { @@ -735,7 +739,6 @@ top: } } out: - checkkwd = 0; #ifdef DEBUG if (!alreadyseen) TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));