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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 9 19:11:35 UTC 2021


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

--- Comment #6 from thomas.bockman at gmail.com ---
(In reply to kinke from comment #4)
> (In reply to thomas.bockman from comment #3)
> > Does that mean that passing a D-initialized extern(C++) class instance to
> > C++ code breaks dynamic casting on the C++ side, too?
> 
> AFAIK, yes.

This is a very serious problem. It sounds like D will turn any attempt by C++
code to dynamically cast a D-allocated class instance into a buffer
overrun-like bug by trying to access meta-data that isn't actually present, and
also possibly by subsequently mis-typing the cast reference (like D does) if
the program doesn't just crash in the first step.

APIs generally do not advertise whether they attempt dynamic casts internally
or not, so there is no way to know what C++ libraries are affected by this bug,
short of auditing their source code, which may not even be publicly available.

Making extern(C++) dynamic casts a compile-time error in D code is an
acceptable, albeit disappointing, solution for D code. But, it won't solve the
problem of D code poisoning linked C++ code. Is there ANY way to solve that
problem, short of fully implementing this feature on the D side in a compatible
way?

The only other way I can think of would be to forbid initialization of
extern(C++) classes on the D side entirely, at least in @safe code. I suspect
that would be a massive and frustrating breaking change for many people,
though.

--


More information about the Digitalmars-d-bugs mailing list