Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: 'case' pattern matching bug with bracket expressions



On Thu, 14 May 2015 16:47:49 +0100
Martijn Dekker <martijn@xxxxxxxx> wrote:
> What confounded me is that this applies even if the pattern is [$var] or
> even ["$var"], where $var is empty. This causes the bracket pattern to
> unexpectedly swallow anything that follows it if $var is empty. That
> seems very unexpected and undesirable to me, particularly since, as far
> as I know, zsh appears to be the only shell that chooses to handle it
> this way.

Sorry about the multiple emails...

I don't *think* the following patch makes anything worse, but notice
you're in any case on fairly soggy ground here, even in bash:

$ var=
$ [[ ']' = [$var]*[$var] ]] && echo matches
matches

and presumably Chet would agree with me that's required by the
standard.

pws

diff --git a/Src/pattern.c b/Src/pattern.c
index 05dcb29..4e5e8a1 100644
--- a/Src/pattern.c
+++ b/Src/pattern.c
@@ -1405,7 +1405,16 @@ patcomppiece(int *flagp, int paren)
 		starter = patnode(P_ANYBUT);
 	    } else
 		starter = patnode(P_ANYOF);
-	    if (*patparse == Outbrack) {
+	    /*
+	     * []...] means match a "]" or other included characters.
+	     * However, to be a bit helpful and for compatibility
+	     * with other shells, don't take it in that sense if
+	     * there's no further active "]".  That's still imperfect,
+	     * but it's all we can do --- we're required to
+	     * treat [$var]*[$var]with empty var as [ ... ]
+	     * containing "]*[".
+	     */
+	    if (*patparse == Outbrack && strchr(patparse+1, Outbrack)) {
 		patparse++;
 		patadd(NULL, ']', 1, PA_NOALIGN);
 	    }



Messages sorted by: Reverse Date, Date, Thread, Author