public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/96460] New: Warn about signed module that is converted to unsigned value
@ 2020-08-04 15:16 fw at gcc dot gnu.org
2020-08-05 7:23 ` [Bug c/96460] " rguenth at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: fw at gcc dot gnu.org @ 2020-08-04 15:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96460
Bug ID: 96460
Summary: Warn about signed module that is converted to unsigned
value
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: fw at gcc dot gnu.org
Target Milestone: ---
I do not know how many warnings it would generate to warn for code like this
(if i is not known to be non-negative):
unsigned long
f2 (int i)
{
return i % 16;
}
The problem is that if i is negative, i % 16 is also negative, and the result
is a large unsigned value. This is rarely what is intended. Even if the
negative input never occurs, GCC still needs to generate additional code that
is valid for this case.
A fix-hit hint should suggests & for powers of two and a cast to the
appropriate unsigned type for other operands.
I'm not sure what one would write to suppress this warning in cases where it is
inappropriate, maybe (int) i % 16.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/96460] Warn about signed module that is converted to unsigned value
2020-08-04 15:16 [Bug middle-end/96460] New: Warn about signed module that is converted to unsigned value fw at gcc dot gnu.org
@ 2020-08-05 7:23 ` rguenth at gcc dot gnu.org
2020-08-05 7:45 ` fw at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-08-05 7:23 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96460
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|middle-end |c
Severity|normal |enhancement
--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
It's perfectly valid code ... guess similar to -Wconversion though.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/96460] Warn about signed module that is converted to unsigned value
2020-08-04 15:16 [Bug middle-end/96460] New: Warn about signed module that is converted to unsigned value fw at gcc dot gnu.org
2020-08-05 7:23 ` [Bug c/96460] " rguenth at gcc dot gnu.org
@ 2020-08-05 7:45 ` fw at gcc dot gnu.org
2020-08-06 23:15 ` [Bug c/96460] Warn about signed modulo " egallager at gcc dot gnu.org
2020-08-07 5:22 ` fw at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: fw at gcc dot gnu.org @ 2020-08-05 7:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96460
--- Comment #2 from Florian Weimer <fw at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #1)
> It's perfectly valid code ... guess similar to -Wconversion though.
If the modulo result is never negative, it's not *perfectly* valid because GCC
has to add instructions to produce those negative values, and most programmers
will never realize that this is happening.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/96460] Warn about signed modulo that is converted to unsigned value
2020-08-04 15:16 [Bug middle-end/96460] New: Warn about signed module that is converted to unsigned value fw at gcc dot gnu.org
2020-08-05 7:23 ` [Bug c/96460] " rguenth at gcc dot gnu.org
2020-08-05 7:45 ` fw at gcc dot gnu.org
@ 2020-08-06 23:15 ` egallager at gcc dot gnu.org
2020-08-07 5:22 ` fw at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2020-08-06 23:15 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96460
Eric Gallager <egallager at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |egallager at gcc dot gnu.org
Summary|Warn about signed module |Warn about signed modulo
|that is converted to |that is converted to
|unsigned value |unsigned value
--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
There already is a warning from -Wsign-conversion for it:
$ /usr/local/bin/gcc -c -Wall -Wextra -Wconversion 96460.c
96460.c: In function 'f2':
96460.c:4:12: warning: conversion to 'long unsigned int' from 'int' may change
the sign of the result [-Wsign-conversion]
4 | return i % 16;
| ~~^~~~
$
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/96460] Warn about signed modulo that is converted to unsigned value
2020-08-04 15:16 [Bug middle-end/96460] New: Warn about signed module that is converted to unsigned value fw at gcc dot gnu.org
` (2 preceding siblings ...)
2020-08-06 23:15 ` [Bug c/96460] Warn about signed modulo " egallager at gcc dot gnu.org
@ 2020-08-07 5:22 ` fw at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: fw at gcc dot gnu.org @ 2020-08-07 5:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96460
--- Comment #4 from Florian Weimer <fw at gcc dot gnu.org> ---
(In reply to Eric Gallager from comment #3)
> There already is a warning from -Wsign-conversion for it:
It's for the conversion, not the modulo. My hypothesis is that for the modulo,
the warning is much more helpful and can perhaps even enabled by -Wall.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-08-07 5:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-04 15:16 [Bug middle-end/96460] New: Warn about signed module that is converted to unsigned value fw at gcc dot gnu.org
2020-08-05 7:23 ` [Bug c/96460] " rguenth at gcc dot gnu.org
2020-08-05 7:45 ` fw at gcc dot gnu.org
2020-08-06 23:15 ` [Bug c/96460] Warn about signed modulo " egallager at gcc dot gnu.org
2020-08-07 5:22 ` fw at gcc dot gnu.org
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 read-only IMAP folder(s) and NNTP newsgroup(s).