Skip to contentSkip to conversation

staging: greybus: audio: do not use iterator after list loop

PATCH In review Rahul Rao 2 participants 2 messages Oct 7

Subsystems (3)
lore
Share & tools
lore

Rahul Rao

author
#0 lore raw

PATCH 1/1 staging: greybus: audio: do not use iterator after list loop

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.

---
 drivers/staging/greybus/audio_codec.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/greybus/audio_codec.c +6 -4
index 6daa4e706792..f4c3090684bf 100644
@@ -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) {
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 3 lines · Rahul Rao

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/[email protected]/

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