www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23147] New: Integer division may cause undefined behavior in


          Issue ID: 23147
           Summary: Integer division may cause undefined behavior in  safe
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dlang.org
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla 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

May 29 2022