1
0
mirror of https://github.com/golang/go synced 2024-11-11 22:20:22 -07:00

cmd/compile: disallow converting string to notinheap slice

Unlikely to happen in practice, but easy enough to prevent and might
as well do so for completeness.

Fixes #28243.

Change-Id: I848c3af49cb923f088e9490c6a79373e182fad08
Reviewed-on: https://go-review.googlesource.com/c/142719
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Matthew Dempsky 2018-10-16 15:31:07 -07:00
parent 2764d5ee7b
commit b2c397e537
2 changed files with 18 additions and 2 deletions

View File

@ -696,14 +696,22 @@ func convertop(src *types.Type, dst *types.Type, why *string) Op {
}
// Conversions from regular to go:notinheap are not allowed
// (unless it's unsafe.Pointer). This is a runtime-specific
// rule.
// (unless it's unsafe.Pointer). These are runtime-specific
// rules.
// (a) Disallow (*T) to (*U) where T is go:notinheap but U isn't.
if src.IsPtr() && dst.IsPtr() && dst.Elem().NotInHeap() && !src.Elem().NotInHeap() {
if why != nil {
*why = fmt.Sprintf(":\n\t%v is go:notinheap, but %v is not", dst.Elem(), src.Elem())
}
return 0
}
// (b) Disallow string to []T where T is go:notinheap.
if src.IsString() && dst.IsSlice() && dst.Elem().NotInHeap() && (dst.Elem().Etype == types.Bytetype.Etype || dst.Elem().Etype == types.Runetype.Etype) {
if why != nil {
*why = fmt.Sprintf(":\n\t%v is go:notinheap", dst.Elem())
}
return 0
}
// 1. src can be assigned to dst.
op := assignop(src, dst, why)

View File

@ -46,10 +46,18 @@ type t1 struct{ x int }
//go:notinheap
type t2 t1
//go:notinheap
type t3 byte
//go:notinheap
type t4 rune
var sink interface{}
func i() {
sink = new(t1) // no error
sink = (*t2)(new(t1)) // ERROR "cannot convert(.|\n)*t2 is go:notinheap"
sink = (*t2)(new(struct{ x int })) // ERROR "cannot convert(.|\n)*t2 is go:notinheap"
sink = []t3("foo") // ERROR "cannot convert(.|\n)*t3 is go:notinheap"
sink = []t4("bar") // ERROR "cannot convert(.|\n)*t4 is go:notinheap"
}