From: Bart Schaefer <schaefer@brasslantern.com>
To: Felix Uhl <felix.uhl@outlook.de>
Cc: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: Incorrect evaluation of ~ test in ternary conditional
Date: Fri, 29 Dec 2017 18:40:47 -0800 [thread overview]
Message-ID: <CAH+w=7aguzBsj76z6Bt6o8a6i_2UyzFPh07rjUnfAE1LLNW0UQ@mail.gmail.com> (raw)
In-Reply-To: <DB6P194MB00376583089F5F424CEB64C19D0E0@DB6P194MB0037.EURP194.PROD.OUTLOOK.COM>
Catching up on some old stuff ...
On Mon, Dec 18, 2017 at 7:07 AM, Felix Uhl <felix.uhl@outlook.de> wrote:
>
>> - if (*ss++ == '/' && *ss)
>> + if (*ss && *ss++ == '/' && *ss)
>> arg--;
>>
>> 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?
It does, because it increments ss regardless of whether or not the
first character is '/'. So if ss = "a", (*ss++ == '/' && *ss) is
false because 'a' != '/', but ss now points at '\0' instead of at 'a'.
> So the whole condition checks whether the first character is '/' and
> whether the second character is not 0.
Correct, but it has the side-effect of always advancing ss by one position.
> I'd probably write that as (*ss == '/' && *(++ss)), seems much clearer
> to me.
That avoids the side-effect, but I'm not sure if that side-effect was
intentional.
> I fail to understand why your fix should work, the expressions (*ss &&
> *ss++ == '/') and (*ss++ == '/') on their own are logically equivalent
Not when ss = "". If ss points to empty string, the first of those
does not move ss past the null byte into garbage, but the second one
does. The question is what should happen when ss is neither the empty
string nor begins with a slash.
> to (*ss == '/') and have equivalent side-effects as well.
This is incorrect with respect to the side-effects.
> Did you actually test it?
Yes.
next prev parent reply other threads:[~2017-12-30 2:40 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
2017-12-30 2:40 ` Bart Schaefer [this message]
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='CAH+w=7aguzBsj76z6Bt6o8a6i_2UyzFPh07rjUnfAE1LLNW0UQ@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=felix.uhl@outlook.de \
--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).