ALSA: usb-audio: Fix potential memory leaks at error path for UMP open
authorTakashi Iwai <tiwai@suse.de>
Tue, 5 Sep 2023 05:45:11 +0000 (07:45 +0200)
committerTakashi Iwai <tiwai@suse.de>
Tue, 5 Sep 2023 08:10:48 +0000 (10:10 +0200)
commitb1757fa30ef14f254f4719bf6f7d54a4c8207216
treee90ca4a09ed62205dbbed25d0d92c57013bb4c5a
parent99bf5b0baac941176a6a3d5cef7705b29808de34
ALSA: usb-audio: Fix potential memory leaks at error path for UMP open

The allocation and initialization errors at alloc_midi_urbs() that is
called at MIDI 2.0 / UMP device are supposed to be handled at the
caller side by invoking free_midi_urbs().  However, free_midi_urbs()
loops only for ep->num_urbs entries, and since ep->num_entries wasn't
updated yet at the allocation / init error in alloc_midi_urbs(), this
entry won't be released.

The intention of free_midi_urbs() is to release the whole elements, so
change the loop size to NUM_URBS to scan over all elements for fixing
the missed releases.

Also, the call of free_midi_urbs() is missing at
snd_usb_midi_v2_open().  Although it'll be released later at
reopen/close or disconnection, it's better to release immediately at
the error path.

Fixes: ff49d1df79ae ("ALSA: usb-audio: USB MIDI 2.0 UMP support")
Reported-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Closes: https://lore.kernel.org/r/fc275ed315b9157952dcf2744ee7bdb78defdb5f.1693746347.git.christophe.jaillet@wanadoo.fr
Link: https://lore.kernel.org/r/20230905054511.20502-1-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/usb/midi2.c