Re: [PATCH 2/7] firmware: cs_dsp: Check for valid num_regs in cs_dsp_wseq_multi_write()

From: Fredrik Treven
Date: Tue Feb 04 2025 - 16:51:15 EST


On 2/4/25 04:06, Charles Keepax wrote:
On Fri, Jan 31, 2025 at 01:56:33PM -0600, Fred Treven wrote:
If a value of 0 or below is passed into cs_dsp_wseq_multi_write()
the function will never enter its for loop.

Verify that num_regs passed into the function is valid
and throw a user-visible error if not.

Signed-off-by: Fred Treven <ftreven@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/firmware/cirrus/cs_dsp.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/drivers/firmware/cirrus/cs_dsp.c b/drivers/firmware/cirrus/cs_dsp.c
index 56315b0b5583..aacf6960d1ea 100644
--- a/drivers/firmware/cirrus/cs_dsp.c
+++ b/drivers/firmware/cirrus/cs_dsp.c
@@ -3743,6 +3743,11 @@ int cs_dsp_wseq_multi_write(struct cs_dsp *dsp, struct cs_dsp_wseq *wseq,
{
int i, ret;
+ if (num_regs <= 0) {
+ cs_dsp_err(dsp, "Invalid number of regs: %d\n", num_regs);
+ return -EINVAL;
+ }
+

This feels a little defensive, do we really need to check for
this? Normally num_regs is going to come from an ARRAY_SIZE or
something.

Thanks,
Charles

I see your point. The reason I wanted to add this, though, was because I fell
into the trap myself by mistakenly swapping the op_code input and the num_regs
input.

It then became very difficult to track down the mistake because there was no
condition to catch it in place.

If you feel strongly that I should drop this patch then I am okay with doing so.

Best regards,
Fred