Bug 12863 – Crash in cast_.d on OSX 10.9

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-06-06T02:44:00Z
Last change time
2015-04-02T08:47:22Z
Assigned to
nobody
Creator
tcdknutson

Comments

Comment #0 by tcdknutson — 2014-06-06T02:44:34Z
Tested on OSX 10.9, this will cause a segfault in dmd 2.065 in `cast_.d`, where there should be none (it should just return null) ``` interface Node {} interface WhereCondition {} interface Whereable {} class Where { Whereable left; // lhs this(Whereable left ) { this.left = left; } } class Select : Whereable { WhereCondition[] projection; this(WhereCondition[] projection...) { this.projection = projection; } Where where() { return new Where(this); } } class ATable { Where where() { return (new Select(null)).where(); } } void main() { auto users = new ATable(); auto where = users.where(); WhereCondition var = (cast(Select)where.left).projection[0]; auto var2 = cast(Node) var; } ``` Removing the last line (auto var2 = ...) will cause the crash not to happen, which makes me think the issue is arising when casting to `Node` from type WhereCondition.
Comment #1 by verylonglogin.reg — 2015-04-02T08:47:22Z
`Select` constructor is typesafe variadic function so `projection` array may be constructed on stack and thus contains garbage after function returns, casting this garbage to `Node` fails somewhere in D runtime. To fix the code replace --- this.projection = projection; --- with --- this.projection = projection.dup; // `dup` is added --- P.S. One should be aware of typesafe variadic functions danger and carefully read docs [1]. [1] http://dlang.org/function.html