zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] fix other nofork substitution bugs revealed by tests
Date: Mon, 24 Jul 2023 22:53:52 -0700	[thread overview]
Message-ID: <CAH+w=7Z8Vhp-Q1Q23G1dDYdiJVDQjbpYgy5ztXw8UjDRVpca4g@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 169 bytes --]

As alluded to in workers/51957, nearly all of this is about getting
the PS2 stack right.

This applies on top of, and partly reverses, the lex.c hunk of, workers/51985.

[-- Attachment #2: nofork-tested2.txt --]
[-- Type: text/plain, Size: 2482 bytes --]

diff --git b/Src/lex.c a/Src/lex.c
index 323f8b6ac..1b678794a 100644
--- b/Src/lex.c
+++ a/Src/lex.c
@@ -1421,8 +1421,11 @@ gettokstr(int c, int sub)
   brk:
     if (errflag) {
 	if (in_brace_param) {
-	    while(bct-- >= in_brace_param)
+	    while(bct >= in_brace_param) {
+		if (bct-- == cmdsubst)
+		    cmdpop();
 		cmdpop();
+	    }
 	}
 	return LEXERR;
     }
@@ -1430,8 +1433,11 @@ gettokstr(int c, int sub)
     if (unmatched && !(lexflags & LEXFLAGS_ACTIVE))
 	zerr("unmatched %c", unmatched);
     if (in_brace_param) {
-	while(bct-- >= in_brace_param)
+	while(bct >= in_brace_param) {
+	    if (bct-- == cmdsubst)
+		cmdpop();
 	    cmdpop();
+	}
 	zerr("closing brace expected");
     } else if (unset(IGNOREBRACES) && !sub && lexbuf.len > 1 &&
 	       peek == STRING && lexbuf.ptr[-1] == '}' &&
@@ -1468,7 +1474,7 @@ static int
 dquote_parse(char endchar, int sub)
 {
     int pct = 0, brct = 0, bct = 0, intick = 0, err = 0, cmdsubst = 0;
-    int c;
+    int c, bskip = 0;
     int math = endchar == ')' || endchar == ']' || infor;
     int zlemath = math && zlemetacs > zlemetall + addedx - inbufct;
 
@@ -1541,19 +1547,22 @@ dquote_parse(char endchar, int sub)
 	    if (cmdsubst && !intick) {
 		/* In nofork substitution, tokenize as if unquoted */
 		c = Inbrace;
-		bct++;
+		bskip++;
 	    }
 	    break;
 	case '}':
 	    if (intick || !bct)
 		break;
 	    c = Outbrace;
+	    if (bskip) {
+		bskip--;
+		break;
+	    }
 	    if (bct-- == cmdsubst) {
-		cmdpop();
 		cmdsubst = 0;
-	    }
-	    if (cmdsubst == 0)
 		cmdpop();
+	    }
+	    cmdpop();
 	    break;
 	case '`':
 	    c = Qtick;
@@ -1609,7 +1618,7 @@ dquote_parse(char endchar, int sub)
 	add(c);
 	if (!cmdsubst && c == Inbrace) {
 	    /* Check for ${|...} nofork command substitution */
-	    if ((c = hgetc())) {
+	    if ((c = hgetc()) && !lexstop) {
 		if (c == '|' || inblank(c)) {
 		    cmdsubst = bct;
 		    cmdpush(CS_CURSH);
@@ -1623,10 +1632,18 @@ dquote_parse(char endchar, int sub)
     if (intick) {
 	cmdpop();
     }
-    if (bct && bct == cmdsubst)
-	cmdpop();
-    while (bct--)
+    while (bct) {
+	if (bct-- == cmdsubst) {
+	    /*
+	     * You would think this is an error, but if we call it one,
+	     * parsestrnoerr() returns nonzero to subst_parse_str() and
+	     * subsequently "bad substitution" is not reported
+	     */
+	    /* err = 1 */
+	    cmdpop();
+	}
 	cmdpop();
+    }
     if (lexstop)
 	err = intick || endchar || err;
     else if (err == 1) {

                 reply	other threads:[~2023-07-25  5:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Z8Vhp-Q1Q23G1dDYdiJVDQjbpYgy5ztXw8UjDRVpca4g@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).