commit c21d1c13987c7082c5157726dcfa4f9715773c5d
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Tue Feb 21 17:08:45 2017 +0100
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Tue Feb 21 17:08:45 2017 +0100

    [cc1] Fix pcompare() with NULL operands
    
    Pcompare() was using eqtype() to see if the type of the operands
    were compatible, but this is an error because a null pointer
    can be compared to any pointer. For this reason the correct
    code must use convert() which currently deals with all the
    possible combinations

diff --git a/cc1/expr.c b/cc1/expr.c
index fbbbafc..e99932d 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -351,23 +351,16 @@ arithmetic(int op, Node *lp, Node *rp)
 static Node *
 pcompare(int op, Node *lp, Node *rp)
 {
-       Node *np;
-       int err = 0;
+       Node *np, *p;
 
        if (lp->type->prop & TINTEGER)
                XCHG(lp, rp, np);
+       else if (eqtype(lp->type, pvoidtype, 1))
+               XCHG(lp, rp, np);
 
-       if (rp->type->prop & TINTEGER) {
-               if (!cmpnode(rp, 0))
-                       err = 1;
-               rp = convert(rp, pvoidtype, 1);
-       } else if (rp->type->op == PTR) {
-               if (!eqtype(lp->type, rp->type, 1))
-                       err = 1;
-       } else {
-               err = 1;
-       }
-       if (err)
+       if ((p = convert(rp, lp->type, 0)) != NULL)
+               rp = p;
+       else
                errorp("incompatible types in comparison");
        return node(op, inttype, lp, rp);
 }
diff --git a/tests/execute/scc-tests.lst b/tests/execute/scc-tests.lst
index 4e8947d..9e66e0f 100644
--- a/tests/execute/scc-tests.lst
+++ b/tests/execute/scc-tests.lst
@@ -105,3 +105,4 @@
 0112-cond.c
 0113-externredecl.c
 0114-shortassig.c
+0115-null-comparision.c

Reply via email to