shithub: scc

Download patch

ref: c6c4762d155cc69863e1dd20176095bfebd901d1
parent: e81cd3b1b198c9b23885be41293d091485a50e4e
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Feb 21 12:08:45 EST 2017

[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

--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -351,23 +351,16 @@
 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);
 }
--- 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