[Issue 16268] New: Wrong optimization in code with integer overflow
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Jul 12 03:50:01 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16268
Issue ID: 16268
Summary: Wrong optimization in code with integer overflow
Product: D
Version: D2
Hardware: x86_64
OS: Linux
Status: NEW
Severity: major
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: guillaume.boucher.d at outlook.com
The following program behaves incorrectly when compiled with "-O":
import std.stdio;
void f(byte x)
{
for (byte i = 0; i <= x && i >= 0; ++i)
{
assert(i >= 0);
writeln(i);
}
}
void main()
{
f(byte.max);
}
Compiled without flags, this program prints the numbers from 0 to 127 and
terminates.
Compiled with "-O", this program prints the numbers from 0 to 127, and then
repeats -128, -127, ..., 127 infinitely. It does not terminate.
The program should print the numbers from 0 to 127 once. Signed overflow is
defined behaviour, therefore "i >= 0" can not be assumed to be true. Note how
even the assertion is ignored.
Same problem happens with short and int instead of byte.
If one replaces 0 with 1, the program prints 0..127, -128..-1 and terminates.
It should only print 1..127.
(DMD v2.071.1)
--
More information about the Digitalmars-d-bugs
mailing list