All of lore.kernel.org
 help / color / mirror / Atom feed
* check idea: warn when mixing signedness in ?: operator (got bitten by this recently)
@ 2021-04-19 10:21 Aurélien Aptel
  2021-04-19 22:00 ` Luc Van Oostenryck
  2021-04-20 12:16 ` Dan Carpenter
  0 siblings, 2 replies; 7+ messages in thread
From: Aurélien Aptel @ 2021-04-19 10:21 UTC (permalink / raw)
  To: linux-sparse

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).

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.

Cheers,
-- 
Aurélien Aptel / SUSE Labs Samba Team
GPG: 1839 CB5F 9F5B FB9B AA97  8C99 03C8 A49B 521B D5D3
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg, DE
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah HRB 247165 (AG München)


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-04-21 13:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.