[PATCH 4/8] ASoC: nau8821: clarify out-of-bounds check

Pierre-Louis Bossart pierre-louis.bossart at linux.intel.com
Mon Oct 25 20:59:29 CEST 2021


cppcheck reports a false positive

sound/soc/codecs/nau8821.c:390:17: error: Array 'dmic_speed_sel[4]'
accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]

  dmic_speed_sel[i].param, dmic_speed_sel[i].val);
                ^
sound/soc/codecs/nau8821.c:378:2: note: After for loop, i has value 4
 for (i = 0 ; i < 4 ; i++)
 ^
sound/soc/codecs/nau8821.c:390:17: note: Array index out of bounds
  dmic_speed_sel[i].param, dmic_speed_sel[i].val);
                ^

While the code is not incorrect, we can deal with the out-of-bounds
check in a clearer way that makes static analysis happy.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart at linux.intel.com>
---
 sound/soc/codecs/nau8821.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/soc/codecs/nau8821.c b/sound/soc/codecs/nau8821.c
index 2757d2eeb48a..2de818377484 100644
--- a/sound/soc/codecs/nau8821.c
+++ b/sound/soc/codecs/nau8821.c
@@ -381,7 +381,7 @@ static int dmic_clock_control(struct snd_soc_dapm_widget *w,
 			speed_selection = dmic_speed_sel[i].val;
 			break;
 		}
-	if (speed_selection < 0)
+	if (i == 4)
 		return -EINVAL;
 
 	dev_dbg(nau8821->dev,
-- 
2.25.1



More information about the Alsa-devel mailing list