Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions fakelibusb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,27 @@ func (f *fakeLibusb) getDevice(handle *libusbDevHandle) *libusbDevice {
return f.handles[handle]
}

func (f *fakeLibusb) exit(*libusbContext) error {
func (f *fakeLibusb) exit(ctx *libusbContext) error {
close(f.submitted)
if got := len(f.ts); got > 0 {
for t := range f.ts {
f.mu.Lock()
for d := range f.devices {
freeDevicePointer(d)
delete(f.devices, d)
}
for h := range f.handles {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like the handling of "handles" should be similar to the transfers - they should not be freed silently, but rather should be reported as a missing close on the handle?

freeDevHandlePointer(h)
delete(f.handles, h)
}
var remaining []*libusbTransfer
for t := range f.ts {
remaining = append(remaining, t)
}
f.mu.Unlock()

freeContextPointer(ctx)

if got := len(remaining); got > 0 {
for _, t := range remaining {
f.free(t)
}
return fmt.Errorf("fakeLibusb has %d remaining transfers that should have been freed", got)
Expand All @@ -156,7 +173,10 @@ func (f *fakeLibusb) open(d *libusbDevice) (*libusbDevHandle, error) {
func (f *fakeLibusb) close(h *libusbDevHandle) {
f.mu.Lock()
defer f.mu.Unlock()
delete(f.handles, h)
if _, ok := f.handles[h]; ok {
delete(f.handles, h)
freeDevHandlePointer(h)
}
}
func (f *fakeLibusb) reset(*libusbDevHandle) error { return nil }
func (f *fakeLibusb) control(*libusbDevHandle, time.Duration, uint8, uint8, uint16, uint16, []byte) (int, error) {
Expand Down Expand Up @@ -274,7 +294,10 @@ func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) {
func (f *fakeLibusb) free(t *libusbTransfer) {
f.mu.Lock()
defer f.mu.Unlock()
delete(f.ts, t)
if _, ok := f.ts[t]; ok {
delete(f.ts, t)
freeFakeTransferPointer(t)
}
}
func (f *fakeLibusb) setIsoPacketLengths(t *libusbTransfer, length uint32) {
f.mu.Lock()
Expand Down
16 changes: 16 additions & 0 deletions libusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,19 @@
func newDevHandlePointer() *libusbDevHandle {
return (*libusbDevHandle)(unsafe.Pointer(C.malloc(1)))
}

func freeDevicePointer(d *libusbDevice) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should go into the test code, we don't touch these pointers directly in non-test code.

Also add a block comment why these helpers are needed, something along the lines of "in tests, the code allocates some C pointers directly, it needs to be able to free them directly too".

C.free(unsafe.Pointer(d))

Check failure on line 571 in libusb.go

View workflow job for this annotation

GitHub Actions / linux

could not determine kind of name for C.free

Check failure on line 571 in libusb.go

View workflow job for this annotation

GitHub Actions / linux

could not determine kind of name for C.free

Check failure on line 571 in libusb.go

View workflow job for this annotation

GitHub Actions / linux

could not determine kind of name for C.free

Check failure on line 571 in libusb.go

View workflow job for this annotation

GitHub Actions / linux

could not determine kind of name for C.free

Check failure on line 571 in libusb.go

View workflow job for this annotation

GitHub Actions / linux

could not determine kind of name for C.free
}

func freeFakeTransferPointer(t *libusbTransfer) {
C.free(unsafe.Pointer(t))
}

func freeContextPointer(c *libusbContext) {
C.free(unsafe.Pointer(c))
}

func freeDevHandlePointer(h *libusbDevHandle) {
C.free(unsafe.Pointer(h))
}
Loading