]> Cypherpunks repositories - gostls13.git/commitdiff
gc: better error for non-calling use of unsafe builtins.
authorLuuk van Dijk <lvd@golang.org>
Wed, 9 Nov 2011 17:30:54 +0000 (18:30 +0100)
committerLuuk van Dijk <lvd@golang.org>
Wed, 9 Nov 2011 17:30:54 +0000 (18:30 +0100)
Fixes #1951

R=rsc
CC=golang-dev
https://golang.org/cl/5372041

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

index 52344e75637edf3c3610c6376c0e315b3ddb5565..faae7bd9eabd84ef67e5406d6e571beae85954ce 100644 (file)
@@ -1247,6 +1247,7 @@ void      queuemethod(Node *n);
 /*
  *     unsafe.c
  */
+int    isunsafebuiltin(Node *n);
 Node*  unsafenmagic(Node *n);
 
 /*
index f84f8440c4d43fdf7801640ff4b684aa031c109f..ed5c35ae011713ae1f8702c5e05389192a35e3ef 100644 (file)
@@ -210,6 +210,10 @@ reswitch:
                        }
                        n->used = 1;
                }
+               if(!(top &Ecall) && isunsafebuiltin(n)) {
+                       yyerror("%N is not an expression, must be called", n);
+                       goto error;
+               }
                ok |= Erv;
                goto ret;
 
index 7504b51c994fa3dc0dcd7d83d0ea35b919a9099a..21496b08cc44601aa8cf85ddec25904b597ef763 100644 (file)
@@ -10,6 +10,7 @@
  * look for
  *     unsafe.Sizeof
  *     unsafe.Offsetof
+ *     unsafe.Alignof
  * rewrite with a constant
  */
 Node*
@@ -22,7 +23,7 @@ unsafenmagic(Node *nn)
        Val val;
        Node *fn;
        NodeList *args;
-       
+
        fn = nn->left;
        args = nn->list;
 
@@ -83,7 +84,7 @@ bad:
        yyerror("invalid expression %N", nn);
        v = 0;
        goto ret;
-       
+
 yes:
        if(args->next != nil)
                yyerror("extra arguments for %S", s);
@@ -97,3 +98,17 @@ ret:
        n->type = types[TUINTPTR];
        return n;
 }
+
+int
+isunsafebuiltin(Node *n)
+{
+       if(n == N || n->op != ONAME || n->sym == S || n->sym->pkg != unsafepkg)
+               return 0;
+       if(strcmp(n->sym->name, "Sizeof") == 0)
+               return 1;
+       if(strcmp(n->sym->name, "Offsetof") == 0)
+               return 1;
+       if(strcmp(n->sym->name, "Alignof") == 0)
+               return 1;
+       return 0;
+}
diff --git a/test/fixedbugs/bug376.go b/test/fixedbugs/bug376.go
new file mode 100644 (file)
index 0000000..1efbeec
--- /dev/null
@@ -0,0 +1,11 @@
+// errchk $G $D/$F.go
+
+// Copyright 2011 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.
+
+// issue 1951
+package foo
+import "unsafe"
+var v = unsafe.Sizeof  // ERROR "must be called"
+