[Issue 23147] New: Integer division may cause undefined behavior in @safe code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 29 16:18:26 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23147

          Issue ID: 23147
           Summary: Integer division may cause undefined behavior in @safe
                    code
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dlang.org
          Assignee: nobody at puremagic.com
          Reporter: snarwin+bugzilla at gmail.com

According to the language spec [1], the behavior of integer division is
undefined when

* the denominator is 0, or
* the .min value of a signed integer type is divided by -1

However, both of these operations are allowed in @safe code. As of DMD 2.100.0,
the following program compiles without errors:

---
int div(int n, int m) @safe { return n/m; }

void main() @safe
{
    auto a = div(1, 0);
    auto b = div(int.min, -1);
}
---

If integer division is to remain allowed in @safe code, the behavior of these
divisions must be defined.

[1] https://dlang.org/spec/expression.html#division

--


More information about the Digitalmars-d-bugs mailing list