Skip to content

Commit b1757fa

Browse files
committed
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: ff49d1d ("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>
1 parent 99bf5b0 commit b1757fa

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

sound/usb/midi2.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void free_midi_urbs(struct snd_usb_midi2_endpoint *ep)
265265

266266
if (!ep)
267267
return;
268-
for (i = 0; i < ep->num_urbs; ++i) {
268+
for (i = 0; i < NUM_URBS; ++i) {
269269
ctx = &ep->urbs[i];
270270
if (!ctx->urb)
271271
break;
@@ -279,6 +279,7 @@ static void free_midi_urbs(struct snd_usb_midi2_endpoint *ep)
279279
}
280280

281281
/* allocate URBs for an EP */
282+
/* the callers should handle allocation errors via free_midi_urbs() */
282283
static int alloc_midi_urbs(struct snd_usb_midi2_endpoint *ep)
283284
{
284285
struct snd_usb_midi2_urb *ctx;
@@ -351,8 +352,10 @@ static int snd_usb_midi_v2_open(struct snd_ump_endpoint *ump, int dir)
351352
return -EIO;
352353
if (ep->direction == STR_OUT) {
353354
err = alloc_midi_urbs(ep);
354-
if (err)
355+
if (err) {
356+
free_midi_urbs(ep);
355357
return err;
358+
}
356359
}
357360
return 0;
358361
}

0 commit comments

Comments
 (0)