diff --git a/usb/fakelibusb_test.go b/usb/fakelibusb_test.go index dab5eee..da58835 100644 --- a/usb/fakelibusb_test.go +++ b/usb/fakelibusb_test.go @@ -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, } diff --git a/usb/libusb.go b/usb/libusb.go index efa0c9a..355ce07 100644 --- a/usb/libusb.go +++ b/usb/libusb.go @@ -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)) +}