]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/gc: unsafe.Pointer constants may only be converted to uintptr
authorJosh Bleecher Snyder <josharian@gmail.com>
Sat, 2 May 2015 02:50:27 +0000 (19:50 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 7 May 2015 23:54:28 +0000 (23:54 +0000)
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>
src/cmd/internal/gc/const.go
test/convlit.go

index ad2915812eb52c2bae8b035131090166975d79f1..5ec54bdffb1728349b879104fbaebe2b10d595db 100644 (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
        }
index 8a6145d2a0bed8cc6623f5f79aa773df26a16f6c..904e1e63b138411c6969d76e7589da2a3bf28121 100644 (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"