Skip to contentSkip to conversation

staging: greybus: audio: check sscanf() result directly

PATCH In review abdelnasser hussein 3 participants 5 messages Jun 14 – Jun 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: greybus: audio: check sscanf() result directly

Smatch warns:

drivers/staging/greybus/audio_codec.c:335 gbaudio_module_update()
warn: sscanf doesn't return error codes

sscanf() returns the number of successfully matched input items, not a negative error code. Compare the return value directly with the expected number of conversions instead of storing it in ret as an error code.

Also remove the redundant else-if check for snd_soc_dapm_aif_out. The widget id is validated earlier in the function, so the remaining branch can only handle snd_soc_dapm_aif_out. This avoids a compiler warning about a potentially uninitialized variable.

---
 drivers/staging/greybus/audio_codec.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/greybus/audio_codec.c +2 -3
index 720aa752e17e..6daa4e706792 100644
@@ -311,8 +311,7 @@ int gbaudio_module_update(struct gbaudio_codec_info *codec,
 	}
 
 	/* parse dai_id from AIF widget's stream_name */
-	ret = sscanf(w->sname, "%s %d %s", intf_name, &dai_id, dir);
-	if (ret < 3) {
+	if (sscanf(w->sname, "%s %d %s", intf_name, &dai_id, dir) != 3) {
 		dev_err(codec->dev, "Error while parsing dai_id for %s\n", w->name);
 		return -EINVAL;
 	}
@@ -323,7 +322,7 @@ int gbaudio_module_update(struct gbaudio_codec_info *codec,
 			ret = gbaudio_module_enable_tx(codec, module, dai_id);
 		else
 			ret = gbaudio_module_disable_tx(module, dai_id);
-	} else if (w->id == snd_soc_dapm_aif_out) {
+	} else {
 		if (enable)
 			ret = gbaudio_module_enable_rx(codec, module, dai_id);
 		else
Signature · 1 line
2.54.0
#1 lore raw
Quoted 5 lines · abdelnasser hussein

Smatch warns:

drivers/staging/greybus/audio_codec.c:335 gbaudio_module_update()
warn: sscanf doesn't return error codes

Quoted 8 lines

sscanf() returns the number of successfully matched input items, not a negative error code. Compare the return value directly with the expected number of conversions instead of storing it in ret as an error code.

Also remove the redundant else-if check for snd_soc_dapm_aif_out. The widget id is validated earlier in the function, so the remaining branch can only handle snd_soc_dapm_aif_out. This avoids a compiler warning about a potentially uninitialized variable.

When you say "also" that implies it should be a separate patch.

Quoted 2 lines

lkp didn't report the smatch warning :(

Quoted 6 lines
---
 drivers/staging/greybus/audio_codec.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

No list of what changed from previous versions?

thanks,

greg k-h

author
#2 lore raw

Hi Greg,

Thanks for the review and the feedback.

Quoted 1 line · Greg Kroah-Hartman

When you say "also" that implies it should be a separate patch.

Understood. I will split these changes into a two-patch series in v3.

Quoted 1 line

lkp didn't report the smatch warning :(

My mistake. I will fix the Reported-by and Closes tags in the next version to correctly reflect the source of the warning.

Quoted 1 line

No list of what changed from previous versions?

I apologize for missing that. I will make sure to include a proper changelog below the '---' line for v3.

I will send the v3 patch series shortly.

Thanks,
Abdelnasser

Quoted 35 lines · Greg Kroah-Hartman
Quoted 5 lines · abdelnasser hussein

Smatch warns:

drivers/staging/greybus/audio_codec.c:335 gbaudio_module_update()
warn: sscanf doesn't return error codes

Quoted 8 lines

sscanf() returns the number of successfully matched input items, not a negative error code. Compare the return value directly with the expected number of conversions instead of storing it in ret as an error code.

Also remove the redundant else-if check for snd_soc_dapm_aif_out. The widget id is validated earlier in the function, so the remaining branch can only handle snd_soc_dapm_aif_out. This avoids a compiler warning about a potentially uninitialized variable.

When you say "also" that implies it should be a separate patch.

Quoted 2 lines

lkp didn't report the smatch warning :(

Quoted 6 lines
---
 drivers/staging/greybus/audio_codec.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

No list of what changed from previous versions?

thanks,

greg k-h

#3 lore raw
In reply to abdelnasser hussein
Quoted 14 lines · abdelnasser hussein

Smatch warns:

drivers/staging/greybus/audio_codec.c:335 gbaudio_module_update()
warn: sscanf doesn't return error codes

sscanf() returns the number of successfully matched input items, not a negative error code. Compare the return value directly with the expected number of conversions instead of storing it in ret as an error code.

Also remove the redundant else-if check for snd_soc_dapm_aif_out. The widget id is validated earlier in the function, so the remaining branch can only handle snd_soc_dapm_aif_out. This avoids a compiler warning about a potentially uninitialized variable.

Quoted 5 lines

---

This static checker warning is triggered when we propagate the return from sscanf().

Quoted 8 lines
 drivers/staging/greybus/audio_codec.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/greybus/audio_codec.c +0 -0
index 720aa752e17e..6daa4e706792 100644

drivers/staging/greybus/audio_codec.c int gbaudio_module_update(struct gbaudio_codec_info *codec, · line 316

Quoted 8 lines
drivers/staging/greybus/audio_codec.c +1 -2
@@ -311,8 +311,7 @@ int gbaudio_module_update(struct gbaudio_codec_info *codec,
 	}
 
 	/* parse dai_id from AIF widget's stream_name */
-	ret = sscanf(w->sname, "%s %d %s", intf_name, &dai_id, dir);
-	if (ret < 3) {
+	if (sscanf(w->sname, "%s %d %s", intf_name, &dai_id, dir) != 3) {
 		dev_err(codec->dev, "Error while parsing dai_id for %s\n", w->name);
 		return -EINVAL;

So this code is fine as-is since it's returning -EINVAL.

drivers/staging/greybus/audio_codec.c int gbaudio_module_update(struct gbaudio_codec_info *codec, · line 325

Quoted 7 lines
drivers/staging/greybus/audio_codec.c +1 -1
 	}
@@ -323,7 +322,7 @@ int gbaudio_module_update(struct gbaudio_codec_info *codec,
 			ret = gbaudio_module_enable_tx(codec, module, dai_id);
 		else
 			ret = gbaudio_module_disable_tx(module, dai_id);
-	} else if (w->id == snd_soc_dapm_aif_out) {
+	} else {

Yes, this is what the static checker is complaining about. It thinks that the if statement might be false. But to a human reader, the else if or the plain else are equivalent. It's just a style choice which way to write it.

I would just leave this as-is since the original code is fine. To be honest, most old static checker warnings are stuff that someone already decided to leave as is.

It's better to write new checks. Just look for simple fixes in git log and then you can vibe code a check. Better to work against the devel branch of smatch.

regards,
dan carpenter

Quoted 3 lines
drivers/staging/greybus/audio_codec.c +0 -0
 		if (enable)
 			ret = gbaudio_module_enable_rx(codec, module, dai_id);
 		else
author
#4 lore raw

Hi Dan,

Thanks a lot for the explanation and the valuable information. I will definitely take your advice, learn from this, and focus on tackling more important issues going forward.

Best regards,
Abdelnasser

Quoted 73 lines · Dan Carpenter
Quoted 14 lines · abdelnasser hussein

Smatch warns:

drivers/staging/greybus/audio_codec.c:335 gbaudio_module_update()
warn: sscanf doesn't return error codes

sscanf() returns the number of successfully matched input items, not a negative error code. Compare the return value directly with the expected number of conversions instead of storing it in ret as an error code.

Also remove the redundant else-if check for snd_soc_dapm_aif_out. The widget id is validated earlier in the function, so the remaining branch can only handle snd_soc_dapm_aif_out. This avoids a compiler warning about a potentially uninitialized variable.

Quoted 5 lines

---

This static checker warning is triggered when we propagate the return from sscanf().

Quoted 8 lines
 drivers/staging/greybus/audio_codec.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/greybus/audio_codec.c +0 -0
index 720aa752e17e..6daa4e706792 100644

drivers/staging/greybus/audio_codec.c int gbaudio_module_update(struct gbaudio_codec_info *codec, · line 316

Quoted 8 lines
drivers/staging/greybus/audio_codec.c +1 -2
@@ -311,8 +311,7 @@ int gbaudio_module_update(struct gbaudio_codec_info *codec,
      }
 
      /* parse dai_id from AIF widget's stream_name */
-     ret = sscanf(w->sname, "%s %d %s", intf_name, &dai_id, dir);
-     if (ret < 3) {
+     if (sscanf(w->sname, "%s %d %s", intf_name, &dai_id, dir) != 3) {
              dev_err(codec->dev, "Error while parsing dai_id for %s\n", w->name);
              return -EINVAL;

So this code is fine as-is since it's returning -EINVAL.

drivers/staging/greybus/audio_codec.c int gbaudio_module_update(struct gbaudio_codec_info *codec, · line 325

Quoted 7 lines
drivers/staging/greybus/audio_codec.c +1 -1
      }
@@ -323,7 +322,7 @@ int gbaudio_module_update(struct gbaudio_codec_info *codec,
                      ret = gbaudio_module_enable_tx(codec, module, dai_id);
              else
                      ret = gbaudio_module_disable_tx(module, dai_id);
-     } else if (w->id == snd_soc_dapm_aif_out) {
+     } else {

Yes, this is what the static checker is complaining about. It thinks that the if statement might be false. But to a human reader, the else if or the plain else are equivalent. It's just a style choice which way to write it.

I would just leave this as-is since the original code is fine. To be honest, most old static checker warnings are stuff that someone already decided to leave as is.

It's better to write new checks. Just look for simple fixes in git log and then you can vibe code a check. Better to work against the devel branch of smatch.

regards,
dan carpenter

Quoted 3 lines
drivers/staging/greybus/audio_codec.c +0 -0
              if (enable)
                      ret = gbaudio_module_enable_rx(codec, module, dai_id);
              else