[Issue 15573] mystery crashes in @safe code

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jan 19 16:22:23 PST 2016


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

--- Comment #15 from ag0aep6g at gmail.com ---
A first reduction:

test.d:
----
import std.math: isNaN, trunc;
import std.stdio: writeln;

enum IntFlag {NULL, error}

IntFlag intFlag = IntFlag.NULL;
int dummyVar;

int safeDiv(const byte left, const byte right)
{
    const div0 = (right == 0);
    const posOver = (left == int.min) && (right == -1);

    if(div0)
    {
        intFlag = (posOver ? IntFlag.error : IntFlag.error);
        return 0;
    }
    else return left / right;
}

void main()
{
    const byte[] testValues = [-2];
    foreach(const m; testValues)
    {
        foreach(const n; testValues)
        {
            writeln("", m, "", n);

            const theory = trunc(cast(real)n / cast(real)m);
            const bool thrInval = theory.isNaN;

            intFlag = IntFlag.NULL;

            const practice1 = safeDiv(n, m);
            assert(practice1 == theory); /* fails with -inline -O */

            void require(const bool success)
            {
                if(success) return;

                dummyVar = m;
                dummyVar = n;
            }

            require(!thrInval);
            intFlag = IntFlag.NULL;
        }
    }
}
----

`dmd --version && dmd -inline -O test.d && ./test`:
----
DMD64 D Compiler v2.069-devel-605be4c
Copyright (c) 1999-2015 by Digital Mars written by Walter Bright
-2-2
core.exception.AssertError at test.d(37): Assertion failure
----------------
??:? _d_assert [0x43ab53]
??:? void test.__assert(int) [0x439da4]
??:? _Dmain [0x43924c]
??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x43b192]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x43b0d0]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).runAll() [0x43b14e]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x43b0d0]
??:? _d_run_main [0x43b02d]
??:? main [0x439de5]
??:? __libc_start_main [0x375cea3f]
----

--


More information about the Digitalmars-d-bugs mailing list