[Issue 15573] mystery crashes in @safe code

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


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

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g at gmail.com

--- Comment #19 from ag0aep6g at gmail.com ---
Reduced further, got rid of phobos:

----
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)
        {
            const theory = cast(real)n / cast(real)m;
            const bool thrInval = theory.isNaN;

            intFlag = IntFlag.NULL;

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

            void require(const bool success)
            {
                if(success) return;
                dummyVar = m;
            }

            require(!thrInval);
        }
    }
}

bool isNaN(real x) @nogc @trusted pure nothrow
{
    static assert(real.mant_dig == 64);
    enum ushort EXPMASK = 0x7FFF;
    enum EXPPOS_SHORT = 4;
    const ushort e = EXPMASK & (cast(ushort *)&x)[EXPPOS_SHORT];
    const ulong ps = *cast(ulong *)&x;
    return e == EXPMASK &&
        ps & 0x7FFF_FFFF_FFFF_FFFF;
}
----

--


More information about the Digitalmars-d-bugs mailing list