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" "fmt"
"sync" "sync"
"time" "time"
"unsafe"
) )
var ( var (
@@ -302,12 +301,12 @@ func newFakeLibusb() *fakeLibusb {
handles: make(map[*libusbDevHandle]*libusbDevice), handles: make(map[*libusbDevHandle]*libusbDevice),
claims: make(map[*libusbDevice]map[uint8]bool), 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 // 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 // without using the full USB stack. Since the fake library uses the
// libusbDevice only as an identifier, use arbitrary numbers pretending // libusbDevice only as an identifier, use an arbitrary unique pointer.
// to be pointers. The contents of these pointers is never accessed. // The contents of these pointers is never accessed.
fl.fakeDevices[(*libusbDevice)(unsafe.Pointer(uintptr(i)))] = &fakeDevice{ fl.fakeDevices[(*libusbDevice)(newCPointer())] = &fakeDevice{
desc: d, desc: d,
alt: 0, alt: 0,
} }

View File

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