]> Cypherpunks repositories - gostls13.git/commitdiff
gc: bug292
authorRuss Cox <rsc@golang.org>
Thu, 15 Jul 2010 23:13:47 +0000 (16:13 -0700)
committerRuss Cox <rsc@golang.org>
Thu, 15 Jul 2010 23:13:47 +0000 (16:13 -0700)
Fixes #843.

R=ken2
CC=golang-dev
https://golang.org/cl/1729051

src/cmd/gc/go.h
src/cmd/gc/print.c
src/cmd/gc/typecheck.c
src/cmd/gc/unsafe.c
test/fixedbugs/bug292.go [new file with mode: 0644]

index f7591515fafd09531e267c211260ed13f300db56..81eece47850620d0f0692f173e0b8233b37bc967 100644 (file)
@@ -1145,7 +1145,7 @@ void      typechecklist(NodeList *l, int top);
 /*
  *     unsafe.c
  */
-Node*  unsafenmagic(Node *fn, NodeList *args);
+Node*  unsafenmagic(Node *n);
 
 /*
  *     walk.c
index 8738eb41b59f279c38dc103e45c28af155d4e2e8..74c00cf222ea222d1ae7775dfe0d078002aa5314 100644 (file)
@@ -267,6 +267,7 @@ exprfmt(Fmt *f, Node *n, int prec)
                fmtprint(f, "struct literal");
                break;
 
+       case OXDOT:
        case ODOT:
        case ODOTPTR:
        case ODOTINTER:
index 71be98c487c4637c703f50468a64085d65b2ee1f..85a63124ae184594d082f716d1a196fa8e03abb5 100644 (file)
@@ -690,7 +690,7 @@ reswitch:
         */
        case OCALL:
                l = n->left;
-               if(l->op == ONAME && (r = unsafenmagic(l, n->list)) != N) {
+               if(l->op == ONAME && (r = unsafenmagic(n)) != N) {
                        n = r;
                        goto reswitch;
                }
index dbf6f708a8bf35ad457bf9a2eaafd83eafc0387c..33f37563112b9036da4a585fe3c95e90795ab155 100644 (file)
  * rewrite with a constant
  */
 Node*
-unsafenmagic(Node *fn, NodeList *args)
+unsafenmagic(Node *nn)
 {
        Node *r, *n;
        Sym *s;
        Type *t, *tr;
        long v;
        Val val;
+       Node *fn;
+       NodeList *args;
+       
+       fn = nn->left;
+       args = nn->list;
 
        if(safemode || fn == N || fn->op != ONAME || (s = fn->sym) == S)
                goto no;
@@ -35,13 +40,14 @@ unsafenmagic(Node *fn, NodeList *args)
                defaultlit(&r, T);
                tr = r->type;
                if(tr == T)
-                       goto no;
+                       goto bad;
                v = tr->width;
                goto yes;
        }
        if(strcmp(s->name, "Offsetof") == 0) {
+               typecheck(&r, Erv);
                if(r->op != ODOT && r->op != ODOTPTR)
-                       goto no;
+                       goto bad;
                typecheck(&r, Erv);
                v = r->xoffset;
                goto yes;
@@ -51,7 +57,7 @@ unsafenmagic(Node *fn, NodeList *args)
                defaultlit(&r, T);
                tr = r->type;
                if(tr == T)
-                       goto no;
+                       goto bad;
 
                // make struct { byte; T; }
                t = typ(TSTRUCT);
@@ -70,9 +76,15 @@ unsafenmagic(Node *fn, NodeList *args)
 no:
        return N;
 
+bad:
+       yyerror("invalid expression %#N", nn);
+       v = 0;
+       goto ret;
+       
 yes:
        if(args->next != nil)
                yyerror("extra arguments for %S", s);
+ret:
        // any side effects disappear; ignore init
        val.ctype = CTINT;
        val.u.xval = mal(sizeof(*n->val.u.xval));
diff --git a/test/fixedbugs/bug292.go b/test/fixedbugs/bug292.go
new file mode 100644 (file)
index 0000000..05852cd
--- /dev/null
@@ -0,0 +1,22 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2010 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// http://code.google.com/p/go/issues/detail?id=843
+
+package main
+
+import "unsafe"
+
+type T struct {
+       X, Y uint8
+}
+
+func main() {
+       var t T
+       if unsafe.Offsetof(t.X) != 0 || unsafe.Offsetof(t.Y) != 1 {
+               println("BUG", unsafe.Offsetof(t.X), unsafe.Offsetof(t.Y))
+       }
+}