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

Re: Incorrect evaluation of ~ test in ternary conditional



Catching up on some old stuff ...

On Mon, Dec 18, 2017 at 7:07 AM, Felix Uhl <felix.uhl@xxxxxxxxxx> 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.



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