Bug 20743 – Checked!(int, Abort) does not abort but raise SIGFPE
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-04-17T00:08:05Z
Last change time
2020-04-23T01:01:17Z
Keywords
pull
Assigned to
No Owner
Creator
kdevel
Comments
Comment #0 by kdevel — 2020-04-17T00:08:05Z
```
import std.experimental.checkedint;
alias cint = Checked!(int, Abort);
void works_okay ()
{
cint a = cint.max;
cint b = 1;
cint c = a + b;
}
void raises_sigfpe_instead ()
{
cint a = cint.min;
cint b = -1;
cint c = a / b;
}
void main ()
{
import std.stdio;
foreach (f; [&works_okay, &raises_sigfpe_instead])
try f ();
catch (Throwable e)
writefln ("caught Throwable <%s>", e.msg);
}
```
Application output
Overflow on binary operator: int(2147483647) + const(int)(1)
caught Throwable <Assertion failure>
Overflow on binary operator: int(-2147483648) / const(int)(-1)
Error: program killed by signal 8
Comment #1 by wolframw — 2020-04-21T19:49:41Z
This is caused by Warn.onOverflow (which is called by Abort.onOverflow)
returning the result of a / b. In case of an overflow, the IDIV instruction
triggers a division error which then causes SIGFPE.
On Windows, something similar happens. Although there are no POSIX signals, an
EXCEPTION_INT_OVERFLOW (code 0xC0000095) is thrown (via "Structured Exception
Handling"). This exception is seemingly caught by D runtime translated into a
Throwable that is printed as "Integer Overflow" (so NOT an assertion fault).
Note that this only happens for division. Overflows caused by multiplication
do not behave like this (even though, mathematically speaking, a / -1 = a * -1).
In my opinion, the best way to deal with this would be to specifically check if
the offending calculation was a division, in which case int.min would be
returned (otherwise, keep it as-is, i.e. use the mixin to get a result).
Any other suggestions?
Comment #2 by dlang-bot — 2020-04-22T15:04:56Z
@wolframw created dlang/phobos pull request #7454 "Fix Issue 20743 - Checked!(int, Abort) does not abort but raise SIGFPE" fixing this issue:
- Fix Issue 20743 - Checked!(int, Abort) does not abort but raise SIGFPE
https://github.com/dlang/phobos/pull/7454
Comment #3 by dlang-bot — 2020-04-23T01:01:17Z
dlang/phobos pull request #7454 "Fix Issue 20743 - Checked!(int, Abort) does not abort but raise SIGFPE" was merged into master:
- 658f8aeff6e6a70fe43edb685313e454c1ecff54 by wolframw:
Fix Issue 20743 - Checked!(int, Abort) does not abort but raise SIGFPE
https://github.com/dlang/phobos/pull/7454