Re: [PATCH] Add cast to unsigned char to strverscmp

Christian Franke <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hans-Bernhard Bröker wrote:
> Am 08.11.2024 um 12:41 schrieb Christian Franke:
>> Corinna Vinschen wrote:
>>> On Nov  7 13:58, Joel Sherrill wrote:
>
>>> Given that l and r are unsigned char anyway, the entire expression
>>> is unsigned, so there shouldn't be a sign-compare error in newlib's
>>> version.
>
>> It's actually signed, IIRC due to changing 'unsigned preserving' (K&R 
>> C) to 'value preserving' (C89) implicit conversions.
>
> The reason it's signed is because '1' is a signed integer, so this 
> expression has a type train of:
>
>   ((unsigned char) - (signed int)) < (unsigned int)

The LHS of '<' is signed because both arguments are signed after 
implicit integer promotion:

   ((signed int)(unsigned char) - (signed int)) < (unsigned int)

In traditional K&R C, it was unsigned:

   ((unsigned int)(unsigned char) - (signed int)) < (unsigned int)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.