Skip to content

Commit 1bd338a

Browse files
committed
handles: do not store handles by uintptr
If we store values by uintptrs the GC may try to inspect their values when it kicks in. As the pointers are most likely invalid, this will result in an invalid pointer dereference, causing the program to panic. We can fix this by storing values by an int index value instead, returning pointers to those indices as handles instead.
1 parent d95932c commit 1bd338a

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

handles.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ type HandleList struct {
1111
// stores the Go pointers
1212
handles []interface{}
1313
// indicates which indices are in use
14-
set map[uintptr]bool
14+
set map[int]bool
1515
}
1616

1717
func NewHandleList() *HandleList {
1818
return &HandleList{
1919
handles: make([]interface{}, 5),
20-
set: make(map[uintptr]bool),
20+
set: make(map[int]bool),
2121
}
2222
}
2323

2424
// findUnusedSlot finds the smallest-index empty space in our
2525
// list. You must only run this function while holding a write lock.
26-
func (v *HandleList) findUnusedSlot() uintptr {
26+
func (v *HandleList) findUnusedSlot() int {
2727
for i := 1; i < len(v.handles); i++ {
28-
isUsed := v.set[uintptr(i)]
28+
isUsed := v.set[i]
2929
if !isUsed {
30-
return uintptr(i)
30+
return i
3131
}
3232
}
3333

@@ -36,7 +36,7 @@ func (v *HandleList) findUnusedSlot() uintptr {
3636
slot := len(v.handles)
3737
v.handles = append(v.handles, nil)
3838

39-
return uintptr(slot)
39+
return slot
4040
}
4141

4242
// Track adds the given pointer to the list of pointers to track and
@@ -51,12 +51,12 @@ func (v *HandleList) Track(pointer interface{}) unsafe.Pointer {
5151

5252
v.Unlock()
5353

54-
return unsafe.Pointer(slot)
54+
return unsafe.Pointer(&slot)
5555
}
5656

5757
// Untrack stops tracking the pointer given by the handle
5858
func (v *HandleList) Untrack(handle unsafe.Pointer) {
59-
slot := uintptr(handle)
59+
slot := *(*int)(handle)
6060

6161
v.Lock()
6262

@@ -68,7 +68,7 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) {
6868

6969
// Get retrieves the pointer from the given handle
7070
func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
71-
slot := uintptr(handle)
71+
slot := *(*int)(handle)
7272

7373
v.RLock()
7474

0 commit comments

Comments
 (0)