[Issue 5799] Address-of operator fails on nested conditional operator expression

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 18 16:04:21 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5799


timon.gehr at gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Priority|P2                          |P3


--- Comment #1 from timon.gehr at gmx.ch 2011-04-18 16:00:30 PDT ---
I had a look at the DMD source code and I identified the problem:
expression.c (1326):
Expression *Expression::addressOf(Scope *sc){
    Expression *e;

    //printf("Expression::addressOf()\n");
    e = toLvalue(sc, NULL);
    e = new AddrExp(loc, e);
    e->type = type->pointerTo();
    return e;
}

Note how the instruction e->type = type->pointerTo(); is dependent on the fact
that method toLvalue does not change the type of the expression. However, the
current Implementation of CondExp::toLvalue changes the object while creating
an Lvalue. Disaster strikes because CondExp::toLvalue calls addressOf on it's
two subexpressions. If one or both of them are CondExp, e->type may be
incorrect. The reported bug is an instance of this one.

This can be easily resolved by operating on a copy of the CondExp object in
CondExp::toLvalue instead of on the original object.

Suggested fix:
Replace the current implementation of CondExp::toLvalue in expression.c (11140)

- Expression *CondExp::toLvalue(Scope *sc, Expression *ex)
- {
-    PtrExp *e;
- 
-     // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2)
-     e = new PtrExp(loc, this, type);
- 
-     e1 = e1->addressOf(sc);
-     //e1 = e1->toLvalue(sc, NULL);
- 
-     e2 = e2->addressOf(sc);
-     //e2 = e2->toLvalue(sc, NULL);
- 
-     typeCombine(sc);
- 
-     type = e2->type;
-     return e;
- }

With this one:

+ Expression *CondExp::toLvalue(Scope *sc, Expression *ex)
+ {
+     CondExp *e = (CondExp*)copy();
+ 
+     // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2)
+     e->e1 = e->e1->addressOf(sc);
+     e->e2 = e->e2->addressOf(sc);
+ 
+     e->typeCombine(sc);
+ 
+     e->type = e->e2->type;
+     return new PtrExp(loc, e, type);
+ }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list