Skip to contentSkip to conversation

ASoC: mediatek: mt2701: fix snprintf bounds

PATCH Superseded Rosen Penev 3 participants 4 messages May 19 – May 20

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: mediatek: mt2701: fix snprintf bounds

For whatever reason, GCC is unable to figure out that i2s_num is a single digit number, with MT2701_BASE_CLK_NUM being the maximum value it represents. Add a min() call to help it out and fix W=1 errors regarding snprintf bounds.

---
 sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +3 -1
index 5a2bcf027b4f..43157f218409 100644
@@ -25,6 +25,7 @@ static const char *const base_clks[] = {
 int mt2701_init_clock(struct mtk_base_afe *afe)
 {
 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
+	int i2s_num;
 	int i;
 
 	for (i = 0; i < MT2701_BASE_CLK_NUM; i++) {
@@ -35,8 +36,9 @@ int mt2701_init_clock(struct mtk_base_afe *afe)
 		}
 	}
 
+	i2s_num = min(MT2701_BASE_CLK_NUM, afe_priv->soc->i2s_num);
 	/* Get I2S related clocks */
-	for (i = 0; i < afe_priv->soc->i2s_num; i++) {
+	for (i = 0; i < i2s_num; i++) {
 		struct mt2701_i2s_path *i2s_path = &afe_priv->i2s_path[i];
 		struct clk *i2s_ck;
 		char name[13];
Signature · 1 line
2.54.0
maintainer
#1 lore raw

sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c int mt2701_init_clock(struct mtk_base_afe *afe) · line 41

Quoted 9 lines · Rosen Penev

For whatever reason, GCC is unable to figure out that i2s_num is a single digit number, with MT2701_BASE_CLK_NUM being the maximum value it represents. Add a min() call to help it out and fix W=1 errors regarding snprintf bounds.

sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +2 -1
+	i2s_num = min(MT2701_BASE_CLK_NUM, afe_priv->soc->i2s_num);
 	/* Get I2S related clocks */
-	for (i = 0; i < afe_priv->soc->i2s_num; i++) {
+	for (i = 0; i < i2s_num; i++) {

This makes the code rather obscure and will start silently failing if we ever get more than 9 clocks, we should at least put a build time assert or a warn on in there.

#2 lore raw
Quoted 7 lines · Mark Brown
Quoted 4 lines · Rosen Penev

For whatever reason, GCC is unable to figure out that i2s_num is a single digit number, with MT2701_BASE_CLK_NUM being the maximum value it represents. Add a min() call to help it out and fix W=1 errors regarding snprintf bounds.

Quoted 8 lines

sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c int mt2701_init_clock(struct mtk_base_afe *afe) · line 41

Quoted 4 lines
sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +2 -1
+     i2s_num = min(MT2701_BASE_CLK_NUM, afe_priv->soc->i2s_num);
      /* Get I2S related clocks */
-     for (i = 0; i < afe_priv->soc->i2s_num; i++) {
+     for (i = 0; i < i2s_num; i++) {

This makes the code rather obscure and will start silently failing if we ever get more than 9 clocks, we should at least put a build time assert or a warn on in there.

how so? MT2701_BASE_CLK_NUM would increase.

#3 lore raw
In reply to Rosen Penev
Quoted 23 lines · On Mon, 18 May 2026 18:04:40 -0700 Rosen Penev <[email protected]> wrote:

For whatever reason, GCC is unable to figure out that i2s_num is a single digit number, with MT2701_BASE_CLK_NUM being the maximum value it represents. Add a min() call to help it out and fix W=1 errors regarding snprintf bounds.

---
 sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +1 -0
index 5a2bcf027b4f..43157f218409 100644
@@ -25,6 +25,7 @@ static const char *const base_clks[] = {
 int mt2701_init_clock(struct mtk_base_afe *afe)
 {
 	struct mt2701_afe_private *afe_priv = afe->platform_priv;
+	int i2s_num;
 	int i;
 
 	for (i = 0; i < MT2701_BASE_CLK_NUM; i++) {

sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c int mt2701_init_clock(struct mtk_base_afe *afe) · line 39

Quoted 4 lines
sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +1 -0
@@ -35,8 +36,9 @@ int mt2701_init_clock(struct mtk_base_afe *afe)
 		}
 	}
 
+	i2s_num = min(MT2701_BASE_CLK_NUM, afe_priv->soc->i2s_num);

To me that is backwards, like an 'if' put the variable before the limit.

sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c int mt2701_init_clock(struct mtk_base_afe *afe) · line 41

Quoted 3 lines
sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +1 -1
 	/* Get I2S related clocks */
-	for (i = 0; i < afe_priv->soc->i2s_num; i++) {
+	for (i = 0; i < i2s_num; i++) {

Caching the limit also stops the compiler having to read it every iteration.

-- David

Quoted 3 lines
sound/soc/mediatek/mt2701/mt2701-afe-clock-ctrl.c +0 -0
 		struct mt2701_i2s_path *i2s_path = &afe_priv->i2s_path[i];
 		struct clk *i2s_ck;
 		char name[13];