]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cc: reject unions containing pointers
authorDaniel Morsing <daniel.morsing@gmail.com>
Wed, 22 May 2013 19:13:30 +0000 (21:13 +0200)
committerDaniel Morsing <daniel.morsing@gmail.com>
Wed, 22 May 2013 19:13:30 +0000 (21:13 +0200)
If a union contains a pointer, it will mess up the garbage collector, causing memory corruption.

R=golang-dev, dave, nightlyone, adg, dvyukov, bradfitz, minux.ma, r, iant
CC=golang-dev
https://golang.org/cl/8469043

src/cmd/cc/dcl.c

index edfc7e75a8d957bdd4a4a3feab02d6061cd6b4be..0906971038cfcc4a142ccdee1ae2fdfe589f4a94 100644 (file)
@@ -554,6 +554,28 @@ newlist(Node *l, Node *r)
        return new(OLIST, l, r);
 }
 
+static int
+haspointers(Type *t)
+{
+       Type *fld;
+
+       switch(t->etype) {
+       case TSTRUCT:
+               for(fld = t->link; fld != T; fld = fld->down) {
+                       if(haspointers(fld))
+                               return 1;
+               }
+               return 0;
+       case TARRAY:
+               return haspointers(t->link);
+       case TFUNC:
+       case TIND:
+               return 1;
+       default:
+               return 0;
+       }
+}
+
 void
 sualign(Type *t)
 {
@@ -608,6 +630,9 @@ sualign(Type *t)
                                        diag(Z, "incomplete union element");
                        l->offset = 0;
                        l->shift = 0;
+                       if((debug['q'] || debug['Q']) && haspointers(l))
+                               diag(Z, "precise garbage collector cannot handle unions with pointers");
+
                        o = align(align(0, l, Ael1, &maxal), l, Ael2, &maxal);
                        if(o > w)
                                w = o;