Commit a3dfcf51 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

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: 's avatarDavid Chase <drchase@google.com>
parent 0211d7d7
...@@ -204,6 +204,9 @@ func convlit1(np **Node, t *Type, explicit bool) { ...@@ -204,6 +204,9 @@ func convlit1(np **Node, t *Type, explicit bool) {
} }
case CTINT, CTRUNE, CTFLT, CTCPLX: case CTINT, CTRUNE, CTFLT, CTCPLX:
if n.Type.Etype == TUNSAFEPTR && t.Etype != TUINTPTR {
goto bad
}
ct := int(n.Val.Ctype) ct := int(n.Val.Ctype)
if Isint[et] { if Isint[et] {
switch ct { switch ct {
...@@ -264,8 +267,6 @@ bad: ...@@ -264,8 +267,6 @@ bad:
defaultlit(&n, nil) defaultlit(&n, nil)
*np = n *np = n
} }
return
} }
func copyval(v Val) Val { func copyval(v Val) Val {
...@@ -396,6 +397,11 @@ func overflow(v Val, t *Type) { ...@@ -396,6 +397,11 @@ func overflow(v Val, t *Type) {
return return
} }
// Only uintptrs may be converted to unsafe.Pointer, which cannot overflow.
if t.Etype == TUNSAFEPTR {
return
}
if !doesoverflow(v, t) { if !doesoverflow(v, t) {
return return
} }
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
package main package main
import "unsafe"
// explicit conversion of constants // explicit conversion of constants
var x1 = string(1) var x1 = string(1)
var x2 string = string(1) var x2 string = string(1)
...@@ -18,6 +20,11 @@ var x5 = "a" + string(1) ...@@ -18,6 +20,11 @@ var x5 = "a" + string(1)
var x6 = int(1e100) // ERROR "overflow" var x6 = int(1e100) // ERROR "overflow"
var x7 = float32(1e1000) // 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 // implicit conversions merit scrutiny
var s string var s string
var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot" var bad1 string = 1 // ERROR "conver|incompatible|invalid|cannot"
......
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