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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 9 00:16:20 UTC 2021


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

--- Comment #2 from thomas.bockman at gmail.com ---
(In reply to kinke from comment #1)
> So while getting proper dynamic casts working is probably quite hard, we
> could probably disallow downcasts like this and require an explicit static
> cast (`cast(CC) cast(void*) ca`)?

That would be the bare minimum, to prevent people from unknowingly shooting
themselves in the foot with this. It's an egregious hole in @safe as it stands
now, a violation of the principle of least surprise even in @system code, and
potentially a major security issue anywhere.

So, my recommendation is to make extern(C++) dynamic casts a compile-time error
as soon as possible, and notify potential users through the announce forum that
anyone who might have dynamic extern(C++) class casts in their code needs to
update their compiler as soon as possible to identify latent security issues.

Once that has been done the importance of this bug can be dropped down from
"critical" to "enhancement".

However, it would be really nice to get proper dynamic casts working somehow,
as they are a rather fundamental feature of class systems, and extern(C++) is
required for C++ interop, and for -betterC (my use case). Additionally, people
sometimes prefer extern(C++) classes simply because they are lighter weight
than extern(D) without the monitor pointer and certain virtual functions which
are unwanted for attribute reasons:
    https://forum.dlang.org/post/s263a0$15h$1@digitalmars.com

--


More information about the Digitalmars-d-bugs mailing list