bug in strcasecmp and strncasecmp

Brian Inglis Brian.Inglis@SystematicSW.ab.ca
Mon Feb 17 22:17:19 GMT 2025


On 2025-02-17 03:00, Corinna Vinschen via Cygwin wrote:
> On Feb 16 17:18, Bruno Haible via Cygwin wrote:
>> Per POSIX [1], the functions strcasecmp and strncasecmp should
>> "use the current locale to determine the case of the characters.".
>>
>> [1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/strcasecmp.html
>>
>> This is not what Cygwin does: In the fr_FR.ISO8859-1 locale, the
>> characters 0xE9 and 0xC9 are the same modulo case, but strcasecmp
>> and strncasecmp consider these characters to be different.
> 
> Thanks for your report.
> 
> This is a longstanding problem in newlib.  All four strcasecmp functions
> call tolower on a char without casting them to unsigned.  So tolower is
> called with negativ values if the char is not in the ASCII range.
> 
> Adding a cast fixes that and I just pushed a matching patch.
> 
> I'm just not sure if that's sufficient in the light of POSIX.1-2024.
> The above expression seems to indicate that strcasecmp and friends are
> now expected to work on multibyte codesets like UTF-8.
> 
> I checked the glibc sources and they still do the bytewise tolower twist
> as well, though...

Seems like one need only do that in the POSIX (2024) locale:

"When the LC_CTYPE category of the locale being used is from the POSIX locale, 
these functions shall behave as if the strings had been converted to lowercase 
and then a byte comparison performed, and errno shall not be changed on valid 
input. Otherwise, the results are unspecified."

so an implementation is free to DTRT in human locales, as with say TZ.

-- 
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retrancher  but when there is no more to cut
                                 -- Antoine de Saint-Exupéry


More information about the Cygwin mailing list