Thread overview
[Issue 23147] Integer division may cause undefined behavior in @safe code
May 29, 2022
Paul Backus
May 29, 2022
Susan
Aug 10, 2022
RazvanN
Aug 10, 2022
Paul Backus
Dec 17, 2022
Iain Buclaw
May 29, 2022
https://issues.dlang.org/show_bug.cgi?id=23147

Paul Backus <snarwin+bugzilla@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe

--
May 29, 2022
https://issues.dlang.org/show_bug.cgi?id=23147

Susan <su+dlangissues@angel-island.zone> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |su+dlangissues@angel-island
                   |                            |.zone

--
August 10, 2022
https://issues.dlang.org/show_bug.cgi?id=23147

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305@gmail.com> ---
Usually, Walter argues that @safe refers to memory safety. Yes, I know that in the spec it is stated that @safe code may not lead to undefined behavior, however, you can void initialize a variable (that is not a pointer) in safe code, then I don't see why you would not be able to divide by 0. These can lead to undefined behavior, but not necessarily to memory corruption.

Based on that, this bug report is invalid.

--
August 10, 2022
https://issues.dlang.org/show_bug.cgi?id=23147

--- Comment #2 from Paul Backus <snarwin+bugzilla@gmail.com> ---
Undefined behavior means that all bets are off and literally anything can happen, including memory corruption.

Note that LDC at least optimizes code under the assumption that division by zero never happens, so this is not a theoretical concern. This is demonstrated by the following program, compiled with LDC 1.30.0 using the -O option:

---
bool example(int a, int b)
{
    if (a / b)
    {
        return b == 0;
    }
    else return false;
}

void main()
{
    import std.stdio;

    int a = 1, b = 0;

    writeln(a / b); // nonzero
    writeln(example(a, b)); // false
}
---

Godbolt link: https://godbolt.org/z/WPfx796Y9

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=23147

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

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

--- Comment #3 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dlang.org/issues/4124

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--