Skip to contentSkip to conversation

ALSA: linear: Check frame count for all channels

PATCH In review Sreeraj S Kurup 2 participants 5 messages Nov 5 – Nov 6 Sound

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ALSA: linear: Check frame count for all channels

linear_transfer() checks the available frame count only for the first destination channel.

Check the available frame count of all source and destination channels before converting samples.

Use the smallest available frame count to make sure that conversion does not read or write beyond the channel buffers.

---
v2:
Add missing mailing-list recipients

 sound/core/oss/linear.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
sound/core/oss/linear.c +9 -2
index 6e51cfdca238..7765ac0ec400 100644
@@ -6,6 +6,7 @@
  */
 
 #include <linux/time.h>
+#include <linux/minmax.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include "pcm_plugin.h"
@@ -75,6 +76,8 @@ static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
 			       struct snd_pcm_plugin_channel *dst_channels,
 			       snd_pcm_uframes_t frames)
 {
+	unsigned int channel;
+
 	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
 		return -ENXIO;
 	if (frames == 0)
@@ -92,8 +95,12 @@ static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
 		}
 	}
 #endif
-	if (frames > dst_channels[0].frames)
-		frames = dst_channels[0].frames;
+	for (channel = 0; channel < plugin->src_format.channels; channel++) {
+		frames = min(frames, src_channels[channel].frames);
+
+		frames = min(frames, dst_channels[channel].frames);
+	}
+
 	convert(plugin, src_channels, dst_channels, frames);
 	return frames;
 }
Signature · 1 line
2.55.0
#1 lore raw
Quoted 11 lines · Sreeraj S Kurup

linear_transfer() checks the available frame count only for the first destination channel.

Check the available frame count of all source and destination channels before converting samples.

Use the smallest available frame count to make sure that conversion does not read or write beyond the channel buffers.

Well, the number of the processed frames must be identical for all channels. So this is superfluous.

Or, do you see any real case hitting this?

thanks,

Takashi

#2 lore raw

Hi Takashi,

Thanks for reviewing.

I added this check because I thought the available frame count could potentially differ between channels, similar to how the alignment is checked separately for each channel under CONFIG_SND_DEBUG.

I understand that the ALSA plugin framework normally keeps the processed frame count identical across all channels.

Is there any condition or exceptional case where the available frame count could differ between channels? Or is it guaranteed to be identical by the plugin framework?

If it is guaranteed, I agree that this additional check is unnecessary.

Thanks for your guidance.

Best regards,
Sreeraj

#3 lore raw
Quoted 8 lines · Sreeraj S Kurup

Hi Takashi,

Thanks for reviewing.

I added this check because I thought the available frame count could potentially differ between channels, similar to how the alignment is checked separately for each channel under CONFIG_SND_DEBUG.

Those alignment checks are with snd_BUG_ON(), so it'll give kernel warning with stack trace, i.e. the code shouldn't be triggered. I believe the checks are superfluous, but it's a sanity check for debugging, after all.

Quoted 5 lines

I understand that the ALSA plugin framework normally keeps the processed frame count identical across all channels.

Is there any condition or exceptional case where the available frame count could differ between channels? Or is it guaranteed to be identical by the plugin framework?

The frame count must be identical. So, if any, such a check should be conditionally enabled and trigger a kernel WARNING. It means, if it happens, something must be completely wrong.

thanks,

Takashi

Quoted 6 lines

If it is guaranteed, I agree that this additional check is unnecessary.

Thanks for your guidance.

Best regards,
Sreeraj

#4 lore raw

Hi Takashi,

Thanks for the clarification.

I understand now. Since the frame count must be identical across all channels, taking the minimum is unnecessary. If they ever differ, it indicates an internal inconsistency that should be caught with a warning rather than silently adjusting the frame count.

I'll drop this patch.

Thanks for your time and explanation.

Best regards,
Sreeraj