public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/96317] New: [8/9/10/11] Int compare optimizations make some errors
@ 2020-07-25 9:13 570070308 at qq dot com
2020-07-25 9:17 ` [Bug c/96317] " pinskia at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: 570070308 at qq dot com @ 2020-07-25 9:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96317
Bug ID: 96317
Summary: [8/9/10/11] Int compare optimizations make some errors
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: 570070308 at qq dot com
Target Milestone: ---
for the code:
signed int a=2147483647;
if( (signed int)( (signed int)a + (signed int)1 ) < (signed int)2147483647
)
{
printf("111\n");
}
if( (signed int)( (signed int)a + (signed int)1 ) < (signed int)2147483646
)
{
printf("222\n");
}
signed int b=2147483646;
if( (signed int)( (signed int)a + (signed int)1 ) < (signed int)b )
{
printf("333\n");
}
The result is:
111
333
I have checked the assembly files. It seems that the compiler optimize the
a+1<2147483646 to a<=2147483644. There are some other similar situations in
compartion of <=,>,>=. I think it is better to change the ways or give out a
warning.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/96317] [8/9/10/11] Int compare optimizations make some errors
2020-07-25 9:13 [Bug c/96317] New: [8/9/10/11] Int compare optimizations make some errors 570070308 at qq dot com
@ 2020-07-25 9:17 ` pinskia at gcc dot gnu.org
2020-07-25 9:18 ` pinskia at gcc dot gnu.org
2020-07-26 19:03 ` marxin at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-07-25 9:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96317
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Use -fwrapv or unsigned integers as signed signed integer overflow is
undefined. Use -fsanitizer=undefined will detect this at runtime.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/96317] [8/9/10/11] Int compare optimizations make some errors
2020-07-25 9:13 [Bug c/96317] New: [8/9/10/11] Int compare optimizations make some errors 570070308 at qq dot com
2020-07-25 9:17 ` [Bug c/96317] " pinskia at gcc dot gnu.org
@ 2020-07-25 9:18 ` pinskia at gcc dot gnu.org
2020-07-26 19:03 ` marxin at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-07-25 9:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96317
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |INVALID
Status|UNCONFIRMED |RESOLVED
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/96317] [8/9/10/11] Int compare optimizations make some errors
2020-07-25 9:13 [Bug c/96317] New: [8/9/10/11] Int compare optimizations make some errors 570070308 at qq dot com
2020-07-25 9:17 ` [Bug c/96317] " pinskia at gcc dot gnu.org
2020-07-25 9:18 ` pinskia at gcc dot gnu.org
@ 2020-07-26 19:03 ` marxin at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-07-26 19:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96317
Martin Liška <marxin at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |marxin at gcc dot gnu.org
--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
Or you can use UBSAN to see that:
$ gcc pr96317.c -fsanitize=undefined && ./a.out
pr96317.c:7:9: runtime error: signed integer overflow: 2147483647 + 1 cannot be
represented in type 'int'
111
pr96317.c:18:9: runtime error: signed integer overflow: 2147483647 + 1 cannot
be represented in type 'int'
333
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-07-26 19:03 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-25 9:13 [Bug c/96317] New: [8/9/10/11] Int compare optimizations make some errors 570070308 at qq dot com
2020-07-25 9:17 ` [Bug c/96317] " pinskia at gcc dot gnu.org
2020-07-25 9:18 ` pinskia at gcc dot gnu.org
2020-07-26 19:03 ` marxin 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).