From a69a315dbdc6c4558a469c5da10bb58da20c4d0e Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sat, 29 Apr 2017 00:50:38 +0200 Subject: [PATCH] Verify that interface and alternate numbers are 0-based indices, we rely on this assumption in the config. --- usb/libusb.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/usb/libusb.go b/usb/libusb.go index 9b9dbb6..a9503ae 100644 --- a/usb/libusb.go +++ b/usb/libusb.go @@ -228,7 +228,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { Cap: int(cfg.bNumInterfaces), } c.Interfaces = make([]InterfaceInfo, 0, len(ifaces)) - for _, iface := range ifaces { + for ifNum, iface := range ifaces { if iface.num_altsetting == 0 { continue } @@ -240,7 +240,7 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { Cap: int(iface.num_altsetting), } descs := make([]InterfaceSetting, 0, len(alts)) - for _, alt := range alts { + for altNum, alt := range alts { i := InterfaceSetting{ Number: int(alt.bInterfaceNumber), Alternate: int(alt.bAlternateSetting), @@ -248,6 +248,12 @@ func (libusbImpl) getDeviceDesc(d *libusbDevice) (*Descriptor, error) { SubClass: Class(alt.bInterfaceSubClass), Protocol: Protocol(alt.bInterfaceProtocol), } + if ifNum != i.Number { + return nil, fmt.Errorf("config %d interface at index %d has number %d, USB standard states they should be identical", c.Config, ifNum, i.Number) + } + if altNum != i.Alternate { + return nil, fmt.Errorf("config %d interface %d alternate settings at index %d has number %d, USB standard states they should be identical", c.Config, i.Number, altNum, i.Alternate) + } var ends []C.struct_libusb_endpoint_descriptor *(*reflect.SliceHeader)(unsafe.Pointer(&ends)) = reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(alt.endpoint)),