Re: [PATCH v12 04/10] utf8: teach same_encoding() alternative UTF encoding names
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Mar 16, 2018, 18:19 UTC
- Message-ID
- <CAPig+cS2xRPP9P7GZMgkKzE899AQiMkkpNsGeB-MwmAymAzCiQ@mail.gmail.com>
- In-Reply-To
- <xmqqfu4z3o61.fsf@gitster-ct.c.googlers.com>
On Fri, Mar 16, 2018 at 1:50 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 25 quoted lines
> Eric Sunshine <sunshine@sunshineco.com> writes:
>> However, I'm having a tough time imagining cases in which callers
>> would want same_encoding() to return true if both arguments are NULL,
>> but outright crash if only one is NULL (which is the behavior even
>> before this patch). In other words, same_encoding() takes advantage of
>> is_encoding_utf8() for its convenience, not for its NULL-handling.
>> Given that view, the two explicit is_encoding_utf8() calls in
>> same_encoding() seem redundant once the same_utf_encoding() call is
>> added.
>
> So... does that mean we'd want something like this, or do you have
> something else in mind?
>
> int same_encoding(const char *src, const char *dst)
> {
> static const char utf8[] = "UTF-8";
>
> if (!src)
> src = utf8;
> if (!dst)
> dst = utf8;
> if (same_utf_encoding(src, dst))
> return 1;
> return !strcasecmp(src, dst);
> }I am not proposing anything like that for this patch or patch series. I'm merely asking why, after this patch, same_encoding() still contains the (in my mind) now-unneeded conditional:
if (is_encoding_utf8(src) && is_encoding_utf8(dst))
return 1;If I'm reading the current code correctly, same_encoding() will crash when either (but not both) of its arguments is NULL and the non-NULL argument is not a variation of "UTF-8". If both arguments are NULL, then it won't crash, but I don't believe that it was intentional that it should crash for some NULL cases but not others; rather, not crashing when both are NULL is an _accidental_ side-effect of relying upon is_encoding_utf8().
If I understood him correctly, Lars's justification for retaining the conditional in question even after the patch is to maintain the non-crashing behavior for both arguments being NULL, even though it will continue to crash when only one is NULL. That justification doesn't makes sense to me since I can't imagine clients relying on accidental behavior of sometimes crashing, sometimes not, hence my question to Lars.
As for your snippet above which ensures that 'src' and 'dst' are non-NULL, that (or some variation) would be fine if we ever expect callers of same_encoding() to pass NULL for either of those arguments, but such a fix is outside the scope of this patch and even this patch series (which does not require such a fix), IMHO.