From: Felix Uhl <felix.uhl@outlook.de>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: Incorrect evaluation of ~ test in ternary conditional
Date: Mon, 18 Dec 2017 15:07:53 +0000 [thread overview]
Message-ID: <DB6P194MB00376583089F5F424CEB64C19D0E0@DB6P194MB0037.EURP194.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <CAH+w=7YRkk+Ofnf-HyfmGXZdqO_3OVBCxXusAGEQhVS8+jTcGw@mail.gmail.com>
On 16.12.2017 23:01, Bart Schaefer wrote:
> On Fri, Dec 15, 2017 at 5:55 AM, Felix Uhl <felix.uhl@outlook.de> 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.
>
>
Huh, interesting. Even at 5000 repetitions, I don't get a single wrong
result on the first example (takes about 2 minutes to run though):
$ repeat 5000 do cd && print -nP "%(2~:true:)" ; done
$
I wonder where that discrepancy comes from.
> 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 '~'?
The original implementation doesn't skip the first character, does it?
C operator precedence will treat *ss++ like *(ss++), so if ss is (for
the sake of demonstration) 10, it is incremented to 11, but the postfix
operator returns the old value of 10, which is then dereferenced by *.
So the whole condition checks whether the first character is '/' and
whether the second character is not 0.
I'd probably write that as (*ss == '/' && *(++ss)), seems much clearer
to me.
I fail to understand why your fix should work, the expressions (*ss &&
*ss++ == '/') and (*ss++ == '/') on their own are logically equivalent
to (*ss == '/') and have equivalent side-effects as well.
Did you actually test it?
next prev parent reply other threads:[~2017-12-18 15:08 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-15 13:55 Felix Uhl
2017-12-16 22:01 ` Bart Schaefer
2017-12-18 15:07 ` Felix Uhl [this message]
2017-12-30 2:40 ` Bart Schaefer
2018-01-03 10:44 ` Felix Uhl
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=DB6P194MB00376583089F5F424CEB64C19D0E0@DB6P194MB0037.EURP194.PROD.OUTLOOK.COM \
--to=felix.uhl@outlook.de \
--cc=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).