mirror of https://github.com/golang/go.git
internal/fmtsort: sort the unsafe pointers in map
Currently storing keys that contain unsafe.
Pointer in a map could result inruntime panic when printing the map.
The root cause is that unsafe.Pointer is not comparable.
Fixes #42622.
Change-Id: Ie3bae7ee4945041843b66514de6227212a3da73e
GitHub-Last-Rev: d12d41302e
GitHub-Pull-Request: golang/go#42623
Reviewed-on: https://go-review.googlesource.com/c/go/+/270277
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
96b943a483
commit
0bb6115dd6
|
|
@ -130,7 +130,7 @@ func compare(aVal, bVal reflect.Value) int {
|
||||||
default:
|
default:
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
case reflect.Ptr:
|
case reflect.Ptr, reflect.UnsafePointer:
|
||||||
a, b := aVal.Pointer(), bVal.Pointer()
|
a, b := aVal.Pointer(), bVal.Pointer()
|
||||||
switch {
|
switch {
|
||||||
case a < b:
|
case a < b:
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
var compareTests = [][]reflect.Value{
|
var compareTests = [][]reflect.Value{
|
||||||
|
|
@ -32,6 +33,7 @@ var compareTests = [][]reflect.Value{
|
||||||
ct(reflect.TypeOf(complex128(0+1i)), -1-1i, -1+0i, -1+1i, 0-1i, 0+0i, 0+1i, 1-1i, 1+0i, 1+1i),
|
ct(reflect.TypeOf(complex128(0+1i)), -1-1i, -1+0i, -1+1i, 0-1i, 0+0i, 0+1i, 1-1i, 1+0i, 1+1i),
|
||||||
ct(reflect.TypeOf(false), false, true),
|
ct(reflect.TypeOf(false), false, true),
|
||||||
ct(reflect.TypeOf(&ints[0]), &ints[0], &ints[1], &ints[2]),
|
ct(reflect.TypeOf(&ints[0]), &ints[0], &ints[1], &ints[2]),
|
||||||
|
ct(reflect.TypeOf(unsafe.Pointer(&ints[0])), unsafe.Pointer(&ints[0]), unsafe.Pointer(&ints[1]), unsafe.Pointer(&ints[2])),
|
||||||
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
|
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
|
||||||
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
|
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
|
||||||
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
|
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
|
||||||
|
|
@ -118,6 +120,10 @@ var sortTests = []sortTest{
|
||||||
pointerMap(),
|
pointerMap(),
|
||||||
"PTR0:0 PTR1:1 PTR2:2",
|
"PTR0:0 PTR1:1 PTR2:2",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
unsafePointerMap(),
|
||||||
|
"UNSAFEPTR0:0 UNSAFEPTR1:1 UNSAFEPTR2:2",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
map[toy]string{{7, 2}: "72", {7, 1}: "71", {3, 4}: "34"},
|
map[toy]string{{7, 2}: "72", {7, 1}: "71", {3, 4}: "34"},
|
||||||
"{3 4}:34 {7 1}:71 {7 2}:72",
|
"{3 4}:34 {7 1}:71 {7 2}:72",
|
||||||
|
|
@ -159,6 +165,14 @@ func sprintKey(key reflect.Value) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "PTR???"
|
return "PTR???"
|
||||||
|
case "unsafe.Pointer":
|
||||||
|
ptr := key.Interface().(unsafe.Pointer)
|
||||||
|
for i := range ints {
|
||||||
|
if ptr == unsafe.Pointer(&ints[i]) {
|
||||||
|
return fmt.Sprintf("UNSAFEPTR%d", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "UNSAFEPTR???"
|
||||||
case "chan int":
|
case "chan int":
|
||||||
c := key.Interface().(chan int)
|
c := key.Interface().(chan int)
|
||||||
for i := range chans {
|
for i := range chans {
|
||||||
|
|
@ -185,6 +199,14 @@ func pointerMap() map[*int]string {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unsafePointerMap() map[unsafe.Pointer]string {
|
||||||
|
m := make(map[unsafe.Pointer]string)
|
||||||
|
for i := 2; i >= 0; i-- {
|
||||||
|
m[unsafe.Pointer(&ints[i])] = fmt.Sprint(i)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
func chanMap() map[chan int]string {
|
func chanMap() map[chan int]string {
|
||||||
m := make(map[chan int]string)
|
m := make(map[chan int]string)
|
||||||
for i := 2; i >= 0; i-- {
|
for i := 2; i >= 0; i-- {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue