www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21690] New: Unable to dynamic cast extern(C++) classes

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

          Issue ID: 21690
           Summary: Unable to dynamic cast extern(C++) classes
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: thomas.bockman gmail.com

Since 2.067.1, the program below outputs:

    D cast was dynamic.
    C++ cast was static.

The extern(C++) class casts need to be dynamic, just like the extern(D) casts:

///////////////////////////////////////////////
module app;

import std.stdio;

 safe:

extern(D) {
    abstract class DA {
        char foo();
    }
    class DB : DA {
        override char foo() { return 'B'; }
    }
    class DC : DA {
        override char foo() { return 'C'; }
    }
}

extern(C++) {
    abstract class CA {
        char foo();
    }
    class CB : CA {
        override char foo() { return 'b'; }
    }
    class CC : CA {
        override char foo() { return 'c'; }
    }
}

void main() {
    DB db = new DB;
    DA da = db;
    DC dc = cast(DC) da;
    writeln((dc is null)? "D cast was dynamic." : "D cast was static.");

    CB cb = new CB;
    CA ca = cb;
    CC cc = cast(CC) ca;
    writeln((cc is null)? "C++ cast was dynamic." : "C++ cast was static.");

    return;
}
///////////////////////////////////////////////

This is a critical bug, as the results of accessing an incorrectly typed class
object could, in the general case, be almost anything, including memory
corruption.

--
Mar 08