]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/5g, cmd/6g, cmd/8g: more nil ptr to large struct checks
authorIan Lance Taylor <iant@golang.org>
Wed, 24 Apr 2013 15:13:01 +0000 (08:13 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 24 Apr 2013 15:13:01 +0000 (08:13 -0700)
R=r, ken, khr, daniel.morsing
CC=dsymonds, golang-dev, rickyz
https://golang.org/cl/8925043

src/cmd/5g/cgen.c
src/cmd/6g/cgen.c
src/cmd/8g/cgen.c
test/nilptr.go

index 9e35f9566a44b87461f6152cf96c581fd14ff681..0844e180f65c4e9029ea924a758ea0226588fc25 100644 (file)
@@ -679,6 +679,19 @@ agen(Node *n, Node *res)
 
        case ODOT:
                agen(nl, res);
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(nl->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], N);
+                       gmove(res, &n1);
+                       regalloc(&n2, types[TUINT8], &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gmove(&n1, &n2);
+                       regfree(&n1);
+                       regfree(&n2);
+               }
                if(n->xoffset != 0) {
                        nodconst(&n1, types[TINT32], n->xoffset);
                        regalloc(&n2, n1.type, N);
@@ -694,20 +707,20 @@ agen(Node *n, Node *res)
 
        case ODOTPTR:
                cgen(nl, res);
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(nl->type->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], N);
+                       gmove(res, &n1);
+                       regalloc(&n2, types[TUINT8], &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gmove(&n1, &n2);
+                       regfree(&n1);
+                       regfree(&n2);
+               }
                if(n->xoffset != 0) {
-                       // explicit check for nil if struct is large enough
-                       // that we might derive too big a pointer.
-                       if(nl->type->type->width >= unmappedzero) {
-                               regalloc(&n1, types[tptr], N);
-                               gmove(res, &n1);
-                               regalloc(&n2, types[TUINT8], &n1);
-                               n1.op = OINDREG;
-                               n1.type = types[TUINT8];
-                               n1.xoffset = 0;
-                               gmove(&n1, &n2);
-                               regfree(&n1);
-                               regfree(&n2);
-                       }
                        nodconst(&n1, types[TINT32], n->xoffset);
                        regalloc(&n2, n1.type, N);
                        regalloc(&n3, types[tptr], N);
@@ -777,20 +790,18 @@ igen(Node *n, Node *a, Node *res)
                        regalloc(a, types[tptr], res);
                        cgen(n->left, a);
                }
-               if(n->xoffset != 0) {
-                       // explicit check for nil if struct is large enough
-                       // that we might derive too big a pointer.
-                       if(n->left->type->type->width >= unmappedzero) {
-                               regalloc(&n1, types[tptr], N);
-                               gmove(a, &n1);
-                               regalloc(&n2, types[TUINT8], &n1);
-                               n1.op = OINDREG;
-                               n1.type = types[TUINT8];
-                               n1.xoffset = 0;
-                               gmove(&n1, &n2);
-                               regfree(&n1);
-                               regfree(&n2);
-                       }
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(n->left->type->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], N);
+                       gmove(a, &n1);
+                       regalloc(&n2, types[TUINT8], &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gmove(&n1, &n2);
+                       regfree(&n1);
+                       regfree(&n2);
                }
                a->op = OINDREG;
                a->xoffset = n->xoffset;
index a51c0ca58c171e78d566035928e7e24a22da12ee..32980a50b52c7eaf89fa9c41ca3911f44e48fbfc 100644 (file)
@@ -882,24 +882,35 @@ agen(Node *n, Node *res)
 
        case ODOT:
                agen(nl, res);
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(nl->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], res);
+                       gmove(res, &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gins(ATESTB, nodintconst(0), &n1);
+                       regfree(&n1);
+               }
                if(n->xoffset != 0)
                        ginscon(optoas(OADD, types[tptr]), n->xoffset, res);
                break;
 
        case ODOTPTR:
                cgen(nl, res);
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(nl->type->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], res);
+                       gmove(res, &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gins(ATESTB, nodintconst(0), &n1);
+                       regfree(&n1);
+               }
                if(n->xoffset != 0) {
-                       // explicit check for nil if struct is large enough
-                       // that we might derive too big a pointer.
-                       if(nl->type->type->width >= unmappedzero) {
-                               regalloc(&n1, types[tptr], res);
-                               gmove(res, &n1);
-                               n1.op = OINDREG;
-                               n1.type = types[TUINT8];
-                               n1.xoffset = 0;
-                               gins(ATESTB, nodintconst(0), &n1);
-                               regfree(&n1);
-                       }
                        ginscon(optoas(OADD, types[tptr]), n->xoffset, res);
                }
                break;
@@ -950,16 +961,14 @@ igen(Node *n, Node *a, Node *res)
 
        case ODOTPTR:
                cgenr(n->left, a, res);
-               if(n->xoffset != 0) {
-                       // explicit check for nil if struct is large enough
-                       // that we might derive too big a pointer.
-                       if(n->left->type->type->width >= unmappedzero) {
-                               n1 = *a;
-                               n1.op = OINDREG;
-                               n1.type = types[TUINT8];
-                               n1.xoffset = 0;
-                               gins(ATESTB, nodintconst(0), &n1);
-                       }
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(n->left->type->type->width >= unmappedzero) {
+                       n1 = *a;
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gins(ATESTB, nodintconst(0), &n1);
                }
                a->op = OINDREG;
                a->xoffset += n->xoffset;
index f93be57e557f8b97b719f527ea8528d704f5fa13..b88ea401bb8c90ac672b8f0d3251604c79a37e3e 100644 (file)
@@ -739,6 +739,17 @@ agen(Node *n, Node *res)
 
        case ODOT:
                agen(nl, res);
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(nl->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], res);
+                       gmove(res, &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gins(ATESTB, nodintconst(0), &n1);
+                       regfree(&n1);
+               }
                if(n->xoffset != 0) {
                        nodconst(&n1, types[tptr], n->xoffset);
                        gins(optoas(OADD, types[tptr]), &n1, res);
@@ -750,18 +761,18 @@ agen(Node *n, Node *res)
                if(!isptr[t->etype])
                        fatal("agen: not ptr %N", n);
                cgen(nl, res);
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(nl->type->type->width >= unmappedzero) {
+                       regalloc(&n1, types[tptr], res);
+                       gmove(res, &n1);
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gins(ATESTB, nodintconst(0), &n1);
+                       regfree(&n1);
+               }
                if(n->xoffset != 0) {
-                       // explicit check for nil if struct is large enough
-                       // that we might derive too big a pointer.
-                       if(nl->type->type->width >= unmappedzero) {
-                               regalloc(&n1, types[tptr], res);
-                               gmove(res, &n1);
-                               n1.op = OINDREG;
-                               n1.type = types[TUINT8];
-                               n1.xoffset = 0;
-                               gins(ATESTB, nodintconst(0), &n1);
-                               regfree(&n1);
-                       }
                        nodconst(&n1, types[tptr], n->xoffset);
                        gins(optoas(OADD, types[tptr]), &n1, res);
                }
@@ -825,16 +836,14 @@ igen(Node *n, Node *a, Node *res)
                        regalloc(a, types[tptr], res);
                        cgen(n->left, a);
                }
-               if(n->xoffset != 0) {
-                       // explicit check for nil if struct is large enough
-                       // that we might derive too big a pointer.
-                       if(n->left->type->type->width >= unmappedzero) {
-                               n1 = *a;
-                               n1.op = OINDREG;
-                               n1.type = types[TUINT8];
-                               n1.xoffset = 0;
-                               gins(ATESTB, nodintconst(0), &n1);
-                       }
+               // explicit check for nil if struct is large enough
+               // that we might derive too big a pointer.
+               if(n->left->type->type->width >= unmappedzero) {
+                       n1 = *a;
+                       n1.op = OINDREG;
+                       n1.type = types[TUINT8];
+                       n1.xoffset = 0;
+                       gins(ATESTB, nodintconst(0), &n1);
                }
                a->op = OINDREG;
                a->xoffset += n->xoffset;
index b784914e590af5124570bb5f53f6f85fa4e15b0d..793e9967368089ebd7edf8000edc52c70f107729 100644 (file)
@@ -38,6 +38,8 @@ func main() {
        shouldPanic(p8)
        shouldPanic(p9)
        shouldPanic(p10)
+       shouldPanic(p11)
+       shouldPanic(p12)
 }
 
 func shouldPanic(f func()) {
@@ -130,3 +132,23 @@ func p10() {
        var t *T
        println(t.i) // should crash
 }
+
+type T1 struct {
+       T
+}
+
+type T2 struct {
+       *T1
+}
+
+func p11() {
+       t := &T2{}
+       p := &t.i
+       println(*p)
+}
+
+// ADDR(DOT(IND(p))) needs a check also
+func p12() {
+       var p *T = nil
+       println(*(&((*p).i)))
+}