Using the list iterator variable outside the loop breaks iterator scope semantics and can result in invalid memory access if the loop completes without finding a match.
Introduce a dedicated iterator variable 'iter' for traversing codec->module_list and assign it to 'module' only upon finding a match.
Signed-off-by: Rahul Rao shreeramrahul94@gmail.com --- drivers/staging/greybus/audio_codec.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/drivers/staging/greybus/audio_codec.c b/drivers/staging/greybus/audio_codec.c index 6daa4e706792..f4c3090684bf 100644 --- a/drivers/staging/greybus/audio_codec.c +++ b/drivers/staging/greybus/audio_codec.c @@ -395,7 +395,7 @@ static int gbcodec_hw_params(struct snd_pcm_substream *substream, int ret; u8 sig_bits, channels; u32 format, rate; - struct gbaudio_module_info *module; + struct gbaudio_module_info *module = NULL, *iter; struct gbaudio_data_connection *data; struct gb_bundle *bundle; struct gbaudio_codec_info *codec = dev_get_drvdata(dai->dev); @@ -437,10 +437,12 @@ static int gbcodec_hw_params(struct snd_pcm_substream *substream, format = GB_AUDIO_PCM_FMT_S16_LE;
/* find the data connection */ - list_for_each_entry(module, &codec->module_list, list) { - data = find_data(module, dai->id); - if (data) + list_for_each_entry(iter, &codec->module_list, list) { + data = find_data(iter, dai->id); + if (data) { + module = iter; break; + } }
if (!data) {
On Wed, Oct 07, 2026 at 02:41:04PM +0530, Rahul Rao wrote:
Using the list iterator variable outside the loop breaks iterator scope semantics and can result in invalid memory access if the loop completes without finding a match.
No, it cannot. I rejected another patch like this earlier this week:
https://lore.kernel.org/all/asNhLGdigxSqU5Op@hovoldconsulting.com/
I assume this was something flagged by coccinelle here too, which you should have mentioned in your commit message.
I don't know what the warning message looks like but the term "potential" is key here. You still need to look at the code to determine whether there is an actual issue or not.
Johan
Thanks for the clarification, Johan.
You are right, it was flagged by a tool and I missed verifying it against the actual code logic.
I will drop this patch.
And also will check the code again
Thanks, Rahul
On Wed, 7 Oct 2026 at 3:12 PM, Johan Hovold johan@kernel.org wrote:
On Wed, Oct 07, 2026 at 02:41:04PM +0530, Rahul Rao wrote:
Using the list iterator variable outside the loop breaks iterator scope semantics and can result in invalid memory access if the loop completes without finding a match.
No, it cannot. I rejected another patch like this earlier this week:
https://lore.kernel.org/all/asNhLGdigxSqU5Op@hovoldconsulting.com/I assume this was something flagged by coccinelle here too, which you should have mentioned in your commit message.
I don't know what the warning message looks like but the term "potential" is key here. You still need to look at the code to determine whether there is an actual issue or not.
Johan