From patchwork Tue Apr 28 06:17:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 11513883 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 37C2F14DD for ; Tue, 28 Apr 2020 06:18:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2984520730 for ; Tue, 28 Apr 2020 06:18:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726258AbgD1GSX (ORCPT ); Tue, 28 Apr 2020 02:18:23 -0400 Received: from helcar.hmeau.com ([216.24.177.18]:45346 "EHLO fornost.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726284AbgD1GSV (ORCPT ); Tue, 28 Apr 2020 02:18:21 -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 1jTJWW-0001Fz-GB; Tue, 28 Apr 2020 16:15:17 +1000 Received: by gwarestrin.arnor.me.apana.org.au (sSMTP sendmail emulation); Tue, 28 Apr 2020 16:17:58 +1000 Date: Tue, 28 Apr 2020 16:17:58 +1000 From: Herbert Xu To: Ron Yorston Cc: contact@emersion.fr, dash@vger.kernel.org Subject: [v2 PATCH] parser: Catch errors in expandstr Message-ID: <20200428061758.GA1771@gondor.apana.org.au> References: <20200121063959.tel7ty76fkz33xpn@gondor.apana.org.au> <5e56df24.H24u7mxAPbLPL6fI%rmy@frippery.org> <20200228004023.GA9163@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200228004023.GA9163@gondor.apana.org.au> 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 On Fri, Feb 28, 2020 at 11:40:23AM +1100, Herbert Xu wrote: > On Wed, Feb 26, 2020 at 09:12:04PM +0000, Ron Yorston wrote: > > Herbert Xu wrote: > > >This patch fixes it by using the literal value of PS1 should an > > >error occur during expansion. > > > > There's another case that should be handled. PS1='`xxx(`' causes the > > shell to exit because the old-style backquote leaves an additional file > > on the stack. > > > > Signed-off-by: Ron Yorston > > --- > > src/parser.c | 4 +++- > > 1 file changed, 3 insertions(+), 1 deletion(-) > > When you send a patch to an existing thread could you please change > the Subject line? Otherwise your patch will be silently dropped by > patchwork. As I haven't received another patch, I'm going to fold yours into my original patch. ---8<--- On Fri, Dec 13, 2019 at 02:51:34PM +0000, Simon Ser wrote: > Just noticed another dash bug: when setting invalid PS1 values dash > enters an infinite loop. > > For instance, setting PS1='$(' makes dash print many of these: > > dash: 1: Syntax error: end of file unexpected (expecting ")") > > It would be nice to fallback to the default PS1 value on error. This patch fixes it by using the literal value of PS1 should an error occur during expansion. On Wed, Feb 26, 2020 at 09:12:04PM +0000, Ron Yorston wrote: > > There's another case that should be handled. PS1='`xxx(`' causes the > shell to exit because the old-style backquote leaves an additional file > on the stack. Ron's change has been folded into this patch. Reported-by: Simon Ser Reported-by: Ron Yorston Signed-off-by: Herbert Xu diff --git a/src/parser.c b/src/parser.c index b318b08..5e36929 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1571,28 +1571,46 @@ setprompt(int which) const char * expandstr(const char *ps) { + struct parsefile *volatile file_stop; + struct jmploc *volatile savehandler; + const char *volatile result; + volatile int saveprompt; + struct jmploc jmploc; union node n; - int saveprompt; + int err; + + file_stop = parsefile; /* XXX Fix (char *) cast. */ setinputstring((char *)ps); saveprompt = doprompt; doprompt = 0; + result = ps; + savehandler = handler; + if (unlikely(err = setjmp(jmploc.loc))) + goto out; + handler = &jmploc; readtoken1(pgetc_eatbnl(), DQSYNTAX, FAKEEOFMARK, 0); - doprompt = saveprompt; - - popfile(); - n.narg.type = NARG; n.narg.next = NULL; n.narg.text = wordtext; n.narg.backquote = backquotelist; expandarg(&n, NULL, EXP_QUOTED); - return stackblock(); + result = stackblock(); + +out: + handler = savehandler; + if (err && exception != EXERROR) + longjmp(handler->loc, 1); + + doprompt = saveprompt; + unwindfiles(file_stop); + + return result; } /*