Thread overview
[Issue 5799] New: Address-of operator fails on nested conditional operator expression
Mar 30, 2011
timon.gehr@gmx.ch
Apr 18, 2011
timon.gehr@gmx.ch
Apr 18, 2011
timon.gehr@gmx.ch
Jul 08, 2011
Kenji Hara
Aug 13, 2011
Walter Bright
March 30, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5799

           Summary: Address-of operator fails on nested conditional
                    operator expression
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: timon.gehr@gmx.ch


--- Comment #0 from timon.gehr@gmx.ch 2011-03-30 13:43:22 PDT ---
The following (perfectly valid) D code is rejected by dmd:

int main(){
    int a;
    int *u=&(a ? a : (a ? a : a));
    return 0;
}

Error Message:
minimal.d(3): Error: incompatible types for ((&a) ? (&*(a ? &a : &a))): 'int*'
and 'int**'
minimal.d(3): Error: cannot implicitly convert expression (a ? __error :
(__error)) of type int* to int*

This is nonsense, clearly, the expression
(a ? a : (a ? a : a)) evaluates to a valid int lvalue, therefore it can have
the Address-of operator applied to it.

(For comparison: the following code compiles:
int main(){
    int a;
    (a ? a : (a ? a : a))=a;
    return 0;
})

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5799


timon.gehr@gmx.ch changed:

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


--- Comment #1 from timon.gehr@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: -------
April 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5799


timon.gehr@gmx.ch changed:

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 08, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5799


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg@gmail.com


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2011-07-07 23:38:21 PDT ---
https://github.com/D-Programming-Language/dmd/pull/215

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5799


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2011-08-13 14:58:04 PDT ---
https://github.com/D-Programming-Language/dmd/commit/191125f5ba2d621cf7e21e124b1609c89681e425

https://github.com/D-Programming-Language/dmd/pull/215

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