From patchwork Tue Jan 21 06:39: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: 11343121 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 E85E0924 for ; Tue, 21 Jan 2020 06:40:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C4A8321734 for ; Tue, 21 Jan 2020 06:40:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725972AbgAUGkD (ORCPT ); Tue, 21 Jan 2020 01:40:03 -0500 Received: from helcar.hmeau.com ([216.24.177.18]:59778 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725789AbgAUGkD (ORCPT ); Tue, 21 Jan 2020 01:40:03 -0500 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtps (Exim 4.89 #2 (Debian)) id 1itnCk-00044w-6u; Tue, 21 Jan 2020 14:40:02 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1itnCh-0001Go-G6; Tue, 21 Jan 2020 14:39:59 +0800 Date: Tue, 21 Jan 2020 14:39:59 +0800 From: Herbert Xu To: Simon Ser Cc: "dash@vger.kernel.org" Subject: [PATCH] parser: Catch errors in expandstr Message-ID: <20200121063959.tel7ty76fkz33xpn@gondor.apana.org.au> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org 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. Reported-by: Simon Ser Signed-off-by: Herbert Xu Tested-by: Simon Ser Signed-off-by: Ron Yorston diff --git a/src/parser.c b/src/parser.c index b318b08..201d5bd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1571,28 +1571,43 @@ setprompt(int which) const char * expandstr(const char *ps) { + struct jmploc *volatile savehandler; + const char *volatile result; + volatile int saveprompt; + struct jmploc jmploc; union node n; - int saveprompt; + int err; /* 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; + popfile(); + + return result; } /*