Commit 924ea515 authored by Luuk van Dijk's avatar Luuk van Dijk

gc: better error for non-calling use of unsafe builtins.

Fixes #1951

R=rsc
CC=golang-dev
https://golang.org/cl/5372041
parent 21f50576
...@@ -1247,6 +1247,7 @@ void queuemethod(Node *n); ...@@ -1247,6 +1247,7 @@ void queuemethod(Node *n);
/* /*
* unsafe.c * unsafe.c
*/ */
int isunsafebuiltin(Node *n);
Node* unsafenmagic(Node *n); Node* unsafenmagic(Node *n);
/* /*
......
...@@ -210,6 +210,10 @@ reswitch: ...@@ -210,6 +210,10 @@ reswitch:
} }
n->used = 1; n->used = 1;
} }
if(!(top &Ecall) && isunsafebuiltin(n)) {
yyerror("%N is not an expression, must be called", n);
goto error;
}
ok |= Erv; ok |= Erv;
goto ret; goto ret;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
* look for * look for
* unsafe.Sizeof * unsafe.Sizeof
* unsafe.Offsetof * unsafe.Offsetof
* unsafe.Alignof
* rewrite with a constant * rewrite with a constant
*/ */
Node* Node*
...@@ -97,3 +98,17 @@ ret: ...@@ -97,3 +98,17 @@ ret:
n->type = types[TUINTPTR]; n->type = types[TUINTPTR];
return n; 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;
}
// 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"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment