[Issue 21690] New: Unable to dynamic cast extern(C++) classes

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 8 20:32:14 UTC 2021


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 at puremagic.com
          Reporter: thomas.bockman at 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.

--


More information about the Digitalmars-d-bugs mailing list