From cf19eb7001bde6e373d076280d3aec17aafff142 Mon Sep 17 00:00:00 2001 From: zagrodzki Date: Mon, 4 Sep 2017 12:52:42 +0200 Subject: [PATCH] Fix a race condition in reuse of the same malloc result (#15) In free(), remove Go references to the C memory before the C memory itself is released. That ensures that a concurrent alloc() will not try to insert the same pointer into the map that was not yet removed. --- libusb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libusb.go b/libusb.go index c5218b9..5b16be6 100644 --- a/libusb.go +++ b/libusb.go @@ -467,10 +467,10 @@ func (libusbImpl) data(t *libusbTransfer) (int, TransferStatus) { } func (libusbImpl) free(t *libusbTransfer) { - C.gousb_free_transfer_and_buffer((*C.struct_libusb_transfer)(t)) xferDoneMap.Lock() delete(xferDoneMap.m, t) xferDoneMap.Unlock() + C.gousb_free_transfer_and_buffer((*C.struct_libusb_transfer)(t)) } func (libusbImpl) setIsoPacketLengths(t *libusbTransfer, length uint32) {