assert(false, "...") doesn't terminate program?!

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Oct 27 11:26:09 PDT 2012


I'm investigating issue 8021, and found a strange problem in std.bigint.
Somehow, checkDivByZero is getting called, and isZero correctly detects
a zero denominator, YET the assert passes, and the integer division by
zero doesn't trigger any division by zero signal.

Here's the test case from the bugtracker:

	import std.stdio, std.bigint;

	void main() {
	    BigInt n = BigInt(2) / BigInt(0);
	}

Here's the instrumented Phobos code to show this strange effect (this is
inside struct BigInt, I modified the code to show exactly what the code
is doing, it's pretty strange):

    // Generate a runtime error if division by zero occurs
    //void checkDivByZero() pure const
    void checkDivByZero() const
    {
        import std.stdio;
        writeln(isZero());	// prints true
        writeln(!isZero());	// prints false

	// this should assert, but doesn't?!
        assert(!isZero(), "BigInt division by zero");

        writeln("how did the assert not trigger??!!");	// how did we get here?!

        if (isZero()) {
            auto x = 1/toInt(); // generate a div by zero error

	    // ... or not?
            writeln("div by zero was not triggered!!");
            writeln(toInt());	// prints 0
            assert(0);          // segfaults??!

        }
    }

Output:

	true
	false
	how did the assert not trigger??!!
	div by zero was not triggered!!
	0
	Segmentation fault

It's beyond my imagination how assert(!isZero()) could possibly have
continued execution when isZero() is clearly true and !isZero() is
clearly false. What's going on here??


T

-- 
Tell me and I forget. Teach me and I remember. Involve me and I understand. -- Benjamin Franklin


More information about the Digitalmars-d mailing list