An invalid pointer triggers a runtime failure when passed as argument in

Go1.7:
runtime: bad pointer in frame
github.com/kylelemons/gousb/usb.(*fakeLibusb).open at 0xc420045ce0: 0x1
fatal error: invalid stack pointer

Use an arbitrary C-allocated pointer instead.
This commit is contained in:
Sebastian Zagrodzki
2017-04-09 00:10:15 +02:00
parent 648d3af9d8
commit 3ec748db4c
2 changed files with 9 additions and 6 deletions

View File

@@ -19,7 +19,6 @@ import (
"fmt"
"sync"
"time"
"unsafe"
)
var (
@@ -302,12 +301,12 @@ func newFakeLibusb() *fakeLibusb {
handles: make(map[*libusbDevHandle]*libusbDevice),
claims: make(map[*libusbDevice]map[uint8]bool),
}
for i, d := range fakeDevices {
for _, d := range fakeDevices {
// libusb does not export a way to allocate a new libusb_device struct
// without using the full USB stack. Since the fake library uses the
// libusbDevice only as an identifier, use arbitrary numbers pretending
// to be pointers. The contents of these pointers is never accessed.
fl.fakeDevices[(*libusbDevice)(unsafe.Pointer(uintptr(i)))] = &fakeDevice{
// libusbDevice only as an identifier, use an arbitrary unique pointer.
// The contents of these pointers is never accessed.
fl.fakeDevices[(*libusbDevice)(newCPointer())] = &fakeDevice{
desc: d,
alt: 0,
}

View File

@@ -421,7 +421,11 @@ func xfer_callback(cptr unsafe.Pointer) {
close(ch)
}
// for benchmarking
// for benchmarking and testing
func libusbSetDebug(c *libusbContext, lvl int) {
C.libusb_set_debug((*C.libusb_context)(c), C.int(lvl))
}
func newCPointer() unsafe.Pointer {
return unsafe.Pointer(C.malloc(1))
}