]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: reject embedded unsafe.Pointer values
authorTal Shprecher <tshprecher@gmail.com>
Wed, 20 Apr 2016 21:05:48 +0000 (14:05 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 21 Apr 2016 00:32:01 +0000 (00:32 +0000)
Fixes #14729

Change-Id: Ied819aa7b23e25de30aa8cde049c97297b4cab11
Reviewed-on: https://go-review.googlesource.com/22325
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/dcl.go
src/cmd/compile/internal/gc/type.go
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue14729.go [new file with mode: 0644]

index 0e4b5f605148271cc922cc795f30fa438e9b7a82..e303f11c09648af3649b63a9c52928f3a79118e5 100644 (file)
@@ -719,7 +719,7 @@ func checkembeddedtype(t *Type) {
                }
        }
 
-       if t.IsPtr() {
+       if t.IsPtr() || t.IsUnsafePtr() {
                Yyerror("embedded type cannot be a pointer")
        } else if t.Etype == TFORW && t.ForwardType().Embedlineno == 0 {
                t.ForwardType().Embedlineno = lineno
index 855b070af6f754852b1eedc8ae839b915bc19651..16399547c7769cb2a6a284add54d38ebcb0ede19 100644 (file)
@@ -1111,6 +1111,11 @@ func (t *Type) IsPtr() bool {
        return t.Etype == TPTR32 || t.Etype == TPTR64
 }
 
+// IsUnsafePtr reports whether t is an unsafe pointer.
+func (t *Type) IsUnsafePtr() bool {
+       return t.Etype == TUNSAFEPTR
+}
+
 // IsPtrShaped reports whether t is represented by a single machine pointer.
 // In addition to regular Go pointer types, this includes map, channel, and
 // function types and unsafe.Pointer. It does not include array or struct types
index bf85819bceda8dc5333b28c7d38166b2ff76f8fd..e158c876119d4cab85544fc1e916448b0a81abc5 100644 (file)
@@ -3557,7 +3557,7 @@ func copytype(n *Node, t *Type) {
 
        if embedlineno != 0 {
                lineno = embedlineno
-               if t.IsPtr() {
+               if t.IsPtr() || t.IsUnsafePtr() {
                        Yyerror("embedded type cannot be a pointer")
                }
        }
diff --git a/test/fixedbugs/issue14729.go b/test/fixedbugs/issue14729.go
new file mode 100644 (file)
index 0000000..88e01f9
--- /dev/null
@@ -0,0 +1,14 @@
+// errorcheck
+
+// Copyright 2016 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 14729: structs cannot embed unsafe.Pointer per the spec.
+
+package main
+
+import "unsafe"
+
+type s struct { unsafe.Pointer } // ERROR "embedded type cannot be a pointer"
+type s1 struct { p unsafe.Pointer }