@@ -197,6 +197,7 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint
197197//--------------------------------------------------------------------+
198198bool midih_open (uint8_t rhport , uint8_t dev_addr , tusb_desc_interface_t const * desc_itf , uint16_t max_len ) {
199199 (void ) rhport ;
200+
200201 TU_VERIFY (TUSB_CLASS_AUDIO == desc_itf -> bInterfaceClass );
201202 const uint8_t * p_end = ((const uint8_t * ) desc_itf ) + max_len ;
202203 const uint8_t * p_desc = (const uint8_t * ) desc_itf ;
@@ -210,9 +211,8 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
210211 desc_cb .jack_num = 0 ;
211212
212213 // There can be just a MIDI or an Audio + MIDI interface
213- // If there is Audio Control Interface + Audio Header descriptor, then skip it.
214- // If there is an Audio Control Interface + Audio Streaming Interface, then
215- // ignore the Audio Streaming Interface.
214+ // - If there is Audio Control Interface + Audio Header descriptor, then skip it.
215+ // - If there is an Audio Control Interface + Audio Streaming Interface, then ignore the Audio Streaming Interface.
216216 // Future:
217217 // Note that if this driver is used with an USB Audio Streaming host driver,
218218 // then call that driver first. If the MIDI interface comes before the
@@ -230,19 +230,20 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
230230 p_desc = tu_desc_next (p_desc );
231231 desc_itf = (const tusb_desc_interface_t * )p_desc ;
232232 p_midi -> itf_count = 1 ;
233- // See issue #3159
234- while ((p_desc < p_end ) && ( tu_desc_next ( p_desc ) <= p_end ) && ( desc_itf -> bDescriptorType != TUSB_DESC_INTERFACE || ( desc_itf -> bInterfaceClass == TUSB_CLASS_AUDIO && desc_itf -> bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING )))
235- {
233+ // skip non-interface and non-midi streaming descriptors
234+ while (tu_desc_in_bounds (p_desc , p_end ) &&
235+ ( desc_itf -> bDescriptorType != TUSB_DESC_INTERFACE || ( desc_itf -> bInterfaceClass == TUSB_CLASS_AUDIO && desc_itf -> bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING ))) {
236236 if (desc_itf -> bDescriptorType == TUSB_DESC_INTERFACE && desc_itf -> bAlternateSetting == 0 ) {
237237 p_midi -> itf_count ++ ;
238238 }
239239 p_desc = tu_desc_next (p_desc );
240240 desc_itf = (tusb_desc_interface_t const * )p_desc ;
241241 }
242- TU_VERIFY (p_desc < p_end ); // If MIDI interface comes after Audio Streaming, then max_len did not include the MIDI interface descriptor
242+ TU_VERIFY (p_desc < p_end ); // TODO: If MIDI interface comes after Audio Streaming, then max_len did not include the MIDI interface descriptor
243243 TU_VERIFY (TUSB_CLASS_AUDIO == desc_itf -> bInterfaceClass );
244- }
244+ }
245245 TU_VERIFY (AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf -> bInterfaceSubClass );
246+
246247 TU_LOG_DRV ("MIDI opening Interface %u (addr = %u)\r\n" , desc_itf -> bInterfaceNumber , dev_addr );
247248 p_midi -> bInterfaceNumber = desc_itf -> bInterfaceNumber ;
248249 p_midi -> iInterface = desc_itf -> iInterface ;
@@ -252,7 +253,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
252253 p_desc = tu_desc_next (p_desc ); // next to CS Header
253254
254255 bool found_new_interface = false;
255- while ((p_desc < p_end ) && ( tu_desc_next ( p_desc ) <= p_end ) && !found_new_interface ) {
256+ while (tu_desc_in_bounds (p_desc , p_end ) && !found_new_interface ) {
256257 switch (tu_desc_type (p_desc )) {
257258 case TUSB_DESC_INTERFACE :
258259 found_new_interface = true;
0 commit comments