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

Re: Incorrect evaluation of ~ test in ternary conditional



On Fri, Dec 15, 2017 at 5:55 AM, Felix Uhl <felix.uhl@xxxxxxxxxx> wrote:
> Hi everybody!
>
> Let there be a directory ~/work. Using the ~ test character in a
> conditional ternary prompt will return incorrect results when the
> argument is 2 as shown below:
>
> $ cd && print -P "%(2~:true:false)"
> false
> $ cd work/.. && print -P "%(2~:true:false)"
> true

I actually get randomly incorrect results in the FIRST of your examples:

repeat 5 do cd && print -P "%~ %(2~:true:false)" ; done
~ true
~ false
~ true
~ false
~ false

The second case is consistent but is always wrong:

repeat 5 do cd work/.. && print -P "%~ %(2~:true:false)" ; done
~ true
~ true
~ true
~ true
~ true

Both of the foregoing happen in each of 5.0.2 and the latest
development version.  I don't have 5.0.5 handy.

This seems to be the fix:

diff --git a/Src/prompt.c b/Src/prompt.c
index c478e69..63e8093 100644
--- a/Src/prompt.c
+++ b/Src/prompt.c
@@ -315,7 +315,7 @@ putpromptchar(int doprint, int endchar, unsigned
int *txtchangep)
                case '/':
                case 'C':
                    /* `/' gives 0, `/any' gives 1, etc. */
-                   if (*ss++ == '/' && *ss)
+                   if (*ss && *ss++ == '/' && *ss)
                        arg--;
                    for (; *ss; ss++)
                        if (*ss == '/')

I'm not sure whether (*ss == '/' && *++ss) would be equivalent, i.e.,
I don't know why the original formulation skips over the first
character whether or not it is a '/'.  Possibly to skip a leading '~'?



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