From: Jaroslav Kysela Date: Mon, 30 Nov 2026 13:29:36 +0000 (+0100) Subject: pcm: multi: allow sparse binding maps and fix error-path leak X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=edffc5f7d5ac4a58896014ab430491ff2b080849;p=alsa-lib.git pcm: multi: allow sparse binding maps and fix error-path leak Commit 81c4a89eb445 replaced the binding asserts in snd_pcm_multi_open() with -EINVAL checks, but it rejected sidxs[i] < 0. Sparse binding maps legitimately leave sidxs[i] == -1 for gap (unmapped) channels: _snd_pcm_multi_open() initializes channels_sidx[] to -1 and fills in only the channels that have a bindings entry. Rejecting negative indices broke valid configurations, and the trailing "if (sidxs[i] < 0) continue;" became unreachable dead code. Handle the unmapped case first by storing the slave_idx < 0 sentinel (which the rest of the plugin already treats as "not bound") and skipping the channel. The bounds checks then run only for sidxs[i] >= 0, so the out-of-range read schannels_count[-1] can no longer happen. The early "return -EINVAL" also leaked multi, multi->slaves and multi->channels. Route the binding errors through the existing cleanup block via goto so all allocations are freed. Fixes: 81c4a89eb445 ("pcm: replace multi plugin binding asserts with -EINVAL checks") Closes: https://github.com/alsa-project/alsa-lib/issues/536 Signed-off-by: Jaroslav Kysela --- diff --git a/src/pcm/pcm_multi.c b/src/pcm/pcm_multi.c index c7c67e73..9274a636 100644 --- a/src/pcm/pcm_multi.c +++ b/src/pcm/pcm_multi.c @@ -1084,7 +1084,7 @@ int snd_pcm_multi_open(snd_pcm_t **pcmp, const char *name, snd_pcm_multi_t *multi; unsigned int i; snd_pcm_stream_t stream; - int err; + int err = -EINVAL; assert(pcmp); assert(slaves_count > 0 && slaves_pcm && schannels_count); @@ -1092,9 +1092,8 @@ int snd_pcm_multi_open(snd_pcm_t **pcmp, const char *name, assert(master_slave < slaves_count); multi = calloc(1, sizeof(snd_pcm_multi_t)); - if (!multi) { + if (!multi) return -ENOMEM; - } stream = slaves_pcm[0]->stream; @@ -1121,26 +1120,28 @@ int snd_pcm_multi_open(snd_pcm_t **pcmp, const char *name, } for (i = 0; i < channels_count; ++i) { snd_pcm_multi_channel_t *bind = &multi->channels[i]; - if (sidxs[i] < 0 || sidxs[i] >= (int)slaves_count) { + if (sidxs[i] < 0) { + bind->slave_idx = -1; + continue; + } + if (sidxs[i] >= (int)slaves_count) { SNDERR("multi: ch %d invalid slave idx %d", i, sidxs[i]); - return -EINVAL; + goto out; } if (schannels[i] >= schannels_count[sidxs[i]]) { SNDERR("multi: ch %d channel %d out of range", i, schannels[i]); - return -EINVAL; + goto out; } bind->slave_idx = sidxs[i]; bind->slave_channel = schannels[i]; - if (sidxs[i] < 0) - continue; } - multi->channels_count = channels_count; err = snd_pcm_new(&pcm, SND_PCM_TYPE_MULTI, name, stream, multi->slaves[0].pcm->mode); if (err < 0) { +out: free(multi->slaves); free(multi->channels); free(multi);