Bug 5908 – Optimizer generates wrong value with divide-by-zero.

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-29T00:14:00Z
Last change time
2017-06-26T13:48:07Z
Keywords
pull, wrong-code
Assigned to
yebblies
Creator
kennytm
See also
https://issues.dlang.org/show_bug.cgi?id=13743

Comments

Comment #0 by kennytm — 2011-04-29T00:14:14Z
(See also news://news.digitalmars.com:119/[email protected]) Test case: ----------------------------------------- import std.math, std.conv; void main() { float a = 1.0f; float b = 0.0f; float c = a/b; assert(!isFinite(c), to!string(c)); } ----------------------------------------- $ dmd x $ ./x $ dmd -O x $ ./x [email protected](6): 1.42171e-38 ---------------- 5 x 0x00009495 onAssertErrorMsg + 73 6 x 0x0001343e _d_assert_msg + 26 7 x 0x0000288d _Dmain + 73 8 x 0x00013b3b extern (C) int rt.dmain2.main(int, char**).void runMain() + 23 9 x 0x000136e5 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 29 10 x 0x00013b83 extern (C) int rt.dmain2.main(int, char**).void runAll() + 59 11 x 0x000136e5 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 29 12 x 0x0001367f main + 179 13 x 0x00002839 start + 53 ---------------- ----------------------------------------- The same happens for 'double' and 'real'.
Comment #1 by kennytm — 2011-04-29T00:36:35Z
Actually the whole divide-by-zero scene is broken. With integers this should raise a Floating Point Exception, but instead it is set to the address of some variable in the backend. --------------------------------------------- import std.stdio; void main() { int a = 1; int b = 0; int c = a/b; writefln("%x", c); } --------------------------------------------- $ dmd x $ ./x Floating point exception $ dmd -O x $ ./x 9a9d34 ---------------------------------------------
Comment #2 by jens.k.mueller — 2011-05-05T15:03:25Z
I would have expected that ----------------------------------------- import std.math, std.conv; void main() { float c = 1.0f/0.0f; assert(!isFinite(c), to!string(c)); } ----------------------------------------- fails, too. But it doesn't.
Comment #3 by clugdbug — 2011-05-06T00:46:52Z
In the backend, divide by zero goes to an error message which is commented out.
Comment #4 by kekeniro2 — 2013-11-05T18:50:10Z
Raised Severity. This prevents safe programmming.
Comment #5 by mingodad — 2014-07-20T14:11:42Z
Actually floating point exception doesn't seem to be trapped: Floating point exception (core dumped) -------- import std.stdio; void func1() { int x, z = 2/x; } void func2() { try { func1(); } catch(Exception e) { writeln("catch %s", e.msg);} finally {writeln("finally: func2"); throw new Exception("on func2");} } void func3() { func2(); } void main() { func3(); } --------
Comment #6 by yebblies — 2014-07-24T15:45:37Z
Comment #7 by yebblies — 2014-11-11T12:59:28Z
*** Issue 13569 has been marked as a duplicate of this issue. ***
Comment #8 by github-bugzilla — 2014-11-13T11:22:26Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/8a56471a2ba9f13b4cc966dafb78e5077af92513 Fix Issue 5908 - Optimizer generates wrong value with divide-by-zero. Reinstate optimizer error for integer division by zero https://github.com/D-Programming-Language/dmd/commit/ec66c97df4525b5578bc8601e06eae4ebe9f7ee6 Merge pull request #3810 from yebblies/issue5908 Issue 5908 - Optimizer generates wrong value with divide-by-zero.
Comment #9 by github-bugzilla — 2015-02-18T03:39:30Z
Comment #10 by dlang-bugzilla — 2017-06-26T13:48:07Z
*** Issue 13743 has been marked as a duplicate of this issue. ***