commit dfe85dcaa8a0216b88815c0cf67ef247c51dc18c
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Tue Aug 9 14:06:39 2016 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Tue Aug 9 14:06:39 2016 +0200

    [cc1] Do not allow operations with pointers to incomplete types
    
    The only operations allowed with pointers to incomplete types
    is the assignation and the comparition, so we have to catch
    all the rest and give a good error message.

diff --git a/cc1/expr.c b/cc1/expr.c
index 281ac13..21af200 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -316,7 +316,12 @@ parithmetic(char op, Node *lp, Node *rp)
        if (lp->type->op != PTR)
                XCHG(lp, rp, np);
 
+       tp = rp->type;
+       if (tp->op == PTR && !(tp->type->prop & TDEFINED))
+               goto incomplete;
        tp = lp->type;
+       if (!(tp->type->prop & TDEFINED))
+               goto incomplete;
        size = sizeofnode(tp->type);
 
        if (op == OSUB && BTYPE(rp) == PTR) {
@@ -334,9 +339,13 @@ parithmetic(char op, Node *lp, Node *rp)
 
        return simplify(OADD, tp, lp, rp);
 
+incomplete:
+       errorp("invalid use of undefined type");
+       return lp;
 incorrect:
        errorp("incorrect arithmetic operands");
-       return node(OADD, tp, lp, rp);
+       return lp;
+
 }
 
 static Node *
@@ -542,6 +551,10 @@ incdec(Node *np, char op)
        if (!(tp->prop & TDEFINED)) {
                errorp("invalid use of undefined type");
                return np;
+       } else if (tp->op == PTR && !(tp->type->prop & TDEFINED)) {
+               errorp("%s of pointer to an incomplete type",
+                      (op == OINC) ? "increment" : "decrement");
+               return np;
        } else if (tp->prop & TARITH) {
                inc = constnode(one);
        } else if (tp->op == PTR) {

Reply via email to