1
0
mirror of https://github.com/golang/go synced 2024-11-11 18:51:37 -07:00

cmd/internal/gc: unsafe.Pointer constants may only be converted to uintptr

Fixes #8927.

Change-Id: I638cddd439dd2d4eeef5474118cfcbde0c8a5a43
Reviewed-on: https://go-review.googlesource.com/9632
Run-TryBot: David Chase <drchase@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Josh Bleecher Snyder 2015-05-01 19:50:27 -07:00
parent 0211d7d7b0
commit a3dfcf51c6
2 changed files with 15 additions and 2 deletions

View File

@ -204,6 +204,9 @@ func convlit1(np **Node, t *Type, explicit bool) {
}
case CTINT, CTRUNE, CTFLT, CTCPLX:
if n.Type.Etype == TUNSAFEPTR && t.Etype != TUINTPTR {
goto bad
}
ct := int(n.Val.Ctype)
if Isint[et] {
switch ct {
@ -264,8 +267,6 @@ bad:
defaultlit(&n, nil)
*np = n
}
return
}
func copyval(v Val) Val {
@ -396,6 +397,11 @@ func overflow(v Val, t *Type) {
return
}
// Only uintptrs may be converted to unsafe.Pointer, which cannot overflow.
if t.Etype == TUNSAFEPTR {
return
}
if !doesoverflow(v, t) {
return
}

View File

@ -9,6 +9,8 @@
package main
import "unsafe"
// explicit conversion of constants
var x1 = string(1)
var x2 string = string(1)
@ -18,6 +20,11 @@ var x5 = "a" + string(1)
var x6 = int(1e100) // ERROR "overflow"
var x7 = float32(1e1000) // ERROR "overflow"
// unsafe.Pointer can only convert to/from uintptr
var _ = string(unsafe.Pointer(uintptr(65))) // ERROR "convert"
var _ = float64(unsafe.Pointer(uintptr(65))) // ERROR "convert"
var _ = int(unsafe.Pointer(uintptr(65))) // ERROR "convert"
// implicit conversions merit scrutiny
var s string
var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot"