From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: "Aurélien Aptel" <aaptel@suse.com>
Cc: linux-sparse@vger.kernel.org
Subject: Re: check idea: warn when mixing signedness in ?: operator (got bitten by this recently)
Date: Tue, 20 Apr 2021 00:00:37 +0200 [thread overview]
Message-ID: <20210419220037.orvxzo7hnihcf7rq@mail> (raw)
In-Reply-To: <87wnsyzia4.fsf@suse.com>
On Mon, Apr 19, 2021 at 12:21:39PM +0200, Aurélien Aptel wrote:
> Hi,
>
> If the <then> and <else> expression in the ?: ternary operator have
> different signedness they will both be implicitely casted to unsigned.
>
> When the result is stored in a variable with a storage capable of
> holding both values, this is very unexpected. Consider this example:
>
> int rc = -1;
> unsigned int foo = 123;
> long x = y ? foo : rc;
>
> If one of the branch of the ?: is unsigned, then the compiler will cast
> both branch to unsigned _before_ storing it in x. Despite long being
> able to store INT_MIN, INT_MAX, UINT_MAX (assuming 64b long/32b int).
>
> So if y is 0, it's basically doing
>
> long x = (long)((unsigned int)-1);
>
> Which will result in storing 0x00000000ffffffff (4294967295) instead of
> expected 0xffffffffffffffff (-1).
Hmmm,
I'm wondering what you would be warned about:
- about 'y ? foo : rc' becoming unsigned?
- about the cast 'unsigned int' -> '(signed) long' doing an zero-extension
and not a sign extension?
In both cases, it's not very different than:
int rc = -1;
unsigned int foo = 0;
long x = foo + rc;
and it boils down to the difference between:
long x = (long)((unsigned int)-1);
and
long x = (long)((int)-1);
> I thought we hit some sort of weird compiler bug but after reducing the
> problem to the simple example above and trying it GCC, clang, ICC and
> MSVC they all do the same thing: https://godbolt.org/z/P5Ts7o1df
>
> So it is most likely a C quirk. Standard reads 6.5.15. 5)
> > If both the second and third operands have arithmetic type, the result
> > type that would be determined by the usual arithmetic conversions, were
> > they applied to those two operands, is the type of the result.
Yes, it' also what Sparse is doing.
Cheers,
-- Luc
next prev parent reply other threads:[~2021-04-19 22:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-19 10:21 check idea: warn when mixing signedness in ?: operator (got bitten by this recently) Aurélien Aptel
2021-04-19 22:00 ` Luc Van Oostenryck [this message]
2021-04-20 12:16 ` Dan Carpenter
2021-04-20 12:44 ` Aurélien Aptel
2021-04-21 10:30 ` Dan Carpenter
2021-04-21 13:43 ` Aurélien Aptel
2021-04-21 13:46 ` Dan Carpenter
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=20210419220037.orvxzo7hnihcf7rq@mail \
--to=luc.vanoostenryck@gmail.com \
--cc=aaptel@suse.com \
--cc=linux-sparse@vger.kernel.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.
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).