Skip to content

Commit 0bb6115

Browse files
hanlinsianlancetaylor
authored andcommitted
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: d12d413 GitHub-Pull-Request: #42623 Reviewed-on: https://go-review.googlesource.com/c/go/+/270277 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Bryan C. Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 96b943a commit 0bb6115

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/internal/fmtsort/sort.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func compare(aVal, bVal reflect.Value) int {
130130
default:
131131
return -1
132132
}
133-
case reflect.Ptr:
133+
case reflect.Ptr, reflect.UnsafePointer:
134134
a, b := aVal.Pointer(), bVal.Pointer()
135135
switch {
136136
case a < b:

src/internal/fmtsort/sort_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"strings"
1313
"testing"
14+
"unsafe"
1415
)
1516

1617
var compareTests = [][]reflect.Value{
@@ -32,6 +33,7 @@ var compareTests = [][]reflect.Value{
3233
ct(reflect.TypeOf(complex128(0+1i)), -1-1i, -1+0i, -1+1i, 0-1i, 0+0i, 0+1i, 1-1i, 1+0i, 1+1i),
3334
ct(reflect.TypeOf(false), false, true),
3435
ct(reflect.TypeOf(&ints[0]), &ints[0], &ints[1], &ints[2]),
36+
ct(reflect.TypeOf(unsafe.Pointer(&ints[0])), unsafe.Pointer(&ints[0]), unsafe.Pointer(&ints[1]), unsafe.Pointer(&ints[2])),
3537
ct(reflect.TypeOf(chans[0]), chans[0], chans[1], chans[2]),
3638
ct(reflect.TypeOf(toy{}), toy{0, 1}, toy{0, 2}, toy{1, -1}, toy{1, 1}),
3739
ct(reflect.TypeOf([2]int{}), [2]int{1, 1}, [2]int{1, 2}, [2]int{2, 0}),
@@ -118,6 +120,10 @@ var sortTests = []sortTest{
118120
pointerMap(),
119121
"PTR0:0 PTR1:1 PTR2:2",
120122
},
123+
{
124+
unsafePointerMap(),
125+
"UNSAFEPTR0:0 UNSAFEPTR1:1 UNSAFEPTR2:2",
126+
},
121127
{
122128
map[toy]string{{7, 2}: "72", {7, 1}: "71", {3, 4}: "34"},
123129
"{3 4}:34 {7 1}:71 {7 2}:72",
@@ -159,6 +165,14 @@ func sprintKey(key reflect.Value) string {
159165
}
160166
}
161167
return "PTR???"
168+
case "unsafe.Pointer":
169+
ptr := key.Interface().(unsafe.Pointer)
170+
for i := range ints {
171+
if ptr == unsafe.Pointer(&ints[i]) {
172+
return fmt.Sprintf("UNSAFEPTR%d", i)
173+
}
174+
}
175+
return "UNSAFEPTR???"
162176
case "chan int":
163177
c := key.Interface().(chan int)
164178
for i := range chans {
@@ -185,6 +199,14 @@ func pointerMap() map[*int]string {
185199
return m
186200
}
187201

202+
func unsafePointerMap() map[unsafe.Pointer]string {
203+
m := make(map[unsafe.Pointer]string)
204+
for i := 2; i >= 0; i-- {
205+
m[unsafe.Pointer(&ints[i])] = fmt.Sprint(i)
206+
}
207+
return m
208+
}
209+
188210
func chanMap() map[chan int]string {
189211
m := make(map[chan int]string)
190212
for i := 2; i >= 0; i-- {

0 commit comments

Comments
 (0)