Subject: Re: Completion in empty double-quotes generates error
From: Bart Schaefer <[email-masked]>
To: [email-masked]
Date: Fri, 1 Apr 2016 18:18:24 -0700
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Apr 1,  5:36am, Daniel Shahaf wrote:
} Subject: Re: Completion in empty double-quotes generates error
}
} Bart Schaefer wrote on Wed, Mar 30, 2016 at 11:06:52 -0700:
} > torch% ls ""<backward-char><complete-word>
} > torch% ls " ../../../zsh-5.0/Src/Zle/zle_tricky.c:658: BUG: 0 <= wb (3) <= zlemetacs (2) <= we (3) is not true!
} >        ls ""
} > 
} > If there is even one other character inside the quotes before completing, no
} > error is reported.
} 
} I ran into this six months ago in 37012

I'm not confident this is the same bug, even though the DPUTS() which
trips is the same one.

} there the reproducer was:
} 
} % : 2>1<backward-char><backward-char><<backward-char><complete-word>
} zle_tricky.c:658: BUG: 0 <= wb (4) <= zlemetacs (2) <= we (5) is not true!

I can't reproduce this any longer, at least not with plain compinit and
no zstyles.

In the empty-parens case, "wb" is set to the position of the first
double-quote and "we" is set to just after the second double-quote.
This happens in gotword() called from the lexer from get_comp_string()
at line 1202, via exalias().

Still in get_comp_string(), we then fall into the for-loop commented
/* While building the quoted form, we also clean up the command line. */

This loop deletes the double quotes with foredel() at line 1851, and
decrements we, but never adjusts wb even though the double-quote
at which it points is being deleted.  There's a similar thing with
backdel() in another branch, which presumably could be the 37021 bug,
but I don't know how to trigger it any more.

However, if I try what I think is obvious and set wb = zlemetacs then
the space before the double-quotes gets deleted and never restored.
So either it's not really a problem for zlemetacs to be less than wb,
or the following is correct (and I suppose its possible we need the
same test for wb > we, but I can't make that happen, so I'm leaving
it for now).

diff --git a/Src/Zle/zle_tricky.c b/Src/Zle/zle_tricky.c
index a89b2a3..b1709c1 100644
--- a/Src/Zle/zle_tricky.c
+++ b/Src/Zle/zle_tricky.c
@@ -1849,8 +1849,12 @@ get_comp_string(void)
 		    ocs = zlemetacs;
 		    zlemetacs = i;
 		    foredel(skipchars, CUT_RAW);
-		    if ((zlemetacs = ocs) > --i)
+		    if ((zlemetacs = ocs) > --i) {
 			zlemetacs -= skipchars;
+			/* do not skip past the beginning of the word */
+			if (wb > zlemetacs)
+			    zlemetacs = wb;
+		    }
 		    we -= skipchars;
 		}
 	    } else {
@@ -1861,6 +1865,9 @@ get_comp_string(void)
 		    zlemetacs = we - skipchars;
 		else
 		    zlemetacs = ocs;
+		/* do not skip past the beginning of the word */
+		if (wb > zlemetacs)
+		    zlemetacs = wb;
 		we -= skipchars;
 	    }
 	    /* we need to get rid of all the quotation bits... */