Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/module/audio/sink_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ EXPORT_SYMBOL(sink_get_frame_bytes);

size_t sink_get_free_frames(struct sof_sink *sink)
{
return sink_get_free_size(sink) / sink_get_frame_bytes(sink);
size_t frame_bytes = sink_get_frame_bytes(sink);

/* frame_bytes is channels * sample_size and both are host-influenced;
* guard against a zero divisor (e.g. channels == 0)
*/
if (!frame_bytes)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hot-path call. Should such checks be made at prepare() stage and not on the hot path?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed in principle. Two notes: the guard reuses the frame_bytes value that's already computed one line above and adds a single (well-predicted) branch, so the hot-path cost is essentially nil — the division it protects is the expensive part. But the root cause is channels_count == 0 from base_cfg reaching the audio_stream, and per my reply to Liam there's no central reject for that today. Options: (a) keep this defensive guard, or (b) reject channels_count == 0 once at module/base_cfg validation (a new central check) and drop this. (b) is the cleaner long-term fix but a broader core change. Happy to do (b) if you'd prefer — which way do you want it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reject a frame size of 0 bytes before we get to this call, is there a check higher in the stack that validates this ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked — there isn't a central one. sink_set_channels() stores whatever value it's given (no validation), and the only channel check in the params path (comp_buffer.c) is a mismatch check (stream channels vs params->channels), not a zero check. So a host base_cfg with channels_count == 0 currently propagates down to the audio_stream unguarded, which is what reaches this divide. There is no existing higher-stack reject.

return 0;

return sink_get_free_size(sink) / frame_bytes;
}
EXPORT_SYMBOL(sink_get_free_frames);

Expand Down
Loading