Skip to contentSkip to conversation

ASoC: codes: src4xxx: Avoid clang -Wsometimes-uninitialized in src4xxx_hw_params()

PATCH Superseded Nathan Chancellor 2 participants 4 messages Aug 22, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: codes: src4xxx: Avoid clang -Wsometimes-uninitialized in src4xxx_hw_params()

Clang warns:

sound/soc/codecs/src4xxx.c:280:3: error: variable 'd' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]

                  default:
                  ^~~~~~~
  sound/soc/codecs/src4xxx.c:298:59: note: uninitialized use occurs here
                  ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_11, d);
                                                                          ^
  sound/soc/codecs/src4xxx.c:223:20: note: initialize the variable 'd' to silence this warning
          int val, pj, jd, d;
                            ^
                            = 0
  sound/soc/codecs/src4xxx.c:280:3: error: variable 'jd' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
                  default:
                  ^~~~~~~
  sound/soc/codecs/src4xxx.c:293:59: note: uninitialized use occurs here
                  ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_10, jd);
                                                                          ^~
  sound/soc/codecs/src4xxx.c:223:17: note: initialize the variable 'jd' to silence this warning
          int val, pj, jd, d;
                        ^
                          = 0
  sound/soc/codecs/src4xxx.c:280:3: error: variable 'pj' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
                  default:
                  ^~~~~~~
  sound/soc/codecs/src4xxx.c:288:59: note: uninitialized use occurs here
                  ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_0F, pj);
                                                                          ^~
  sound/soc/codecs/src4xxx.c:223:13: note: initialize the variable 'pj' to silence this warning
          int val, pj, jd, d;
                    ^
                      = 0

3 errors generated.

According to the comment in the default case, other parts of the chip are still functional without these values so just return 0 in the default case to avoid using these variables uninitialized.

---
 sound/soc/codecs/src4xxx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/src4xxx.c +1 -1
index a8f143057b41..cf45caa4bf7f 100644
@@ -283,7 +283,7 @@ static int src4xxx_hw_params(struct snd_pcm_substream *substream,
 			 */
 			dev_info(component->dev,
 				"Couldn't set the RCV PLL as this master clock rate is unknown\n");
-			break;
+			return 0;
 		}
 		ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_0F, pj);
 		if (ret < 0)

base-commit: 94f072748337424c9cf92cd018532a34db3a5516

Signature · 1 line
2.37.2
#1 lore raw

Hi Nathan,

Quoted 12 lines · Nathan Chancellor

Clang warns:

   sound/soc/codecs/src4xxx.c:280:3: error: variable 'd' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
                   default:
                   ^~~~~~~
   sound/soc/codecs/src4xxx.c:298:59: note: uninitialized use occurs here
                   ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_11, d);
                                                                           ^
   sound/soc/codecs/src4xxx.c:223:20: note: initialize the variable 'd' to silence this warning
           int val, pj, jd, d;
                             ^
                             = 0

If you really want to get rid of these warnings, you can use this default for the variables :

pj = 0x0;
jd=0xff;

d = 0xff;

It doesn't really make sense why we would choose to initialise these variables, but if you want to silence the preprocessor, then perhaps those values. Put a message that defaults are not known nor specified in the data sheet and these values are chosen to be unlikely matches of real world values - which ensures regmap updates later from an initial unknown chip state after refresh. All a bit messy really.

Quoted 17 lines

According to the comment in the default case, other parts of the chip are still functional without these values so just return 0 in the default case to avoid using these variables uninitialized.

---
  sound/soc/codecs/src4xxx.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/src4xxx.c +0 -0
index a8f143057b41..cf45caa4bf7f 100644

sound/soc/codecs/src4xxx.c static int src4xxx_hw_params(struct snd_pcm_substream *substream, · line 286

Quoted 5 lines
sound/soc/codecs/src4xxx.c +1 -1
@@ -283,7 +283,7 @@ static int src4xxx_hw_params(struct snd_pcm_substream *substream,
  			 */
  			dev_info(component->dev,
  				"Couldn't set the RCV PLL as this master clock rate is unknown\n");
-			break;
+			return 0;

Don't return here, the rest of the chip is still functional and probably in use. Print the dev_info and continue.

#2 lore raw

Hi Matt,

Quoted 23 lines · Matt Flax

Hi Nathan,

Quoted 12 lines · Nathan Chancellor

Clang warns:

   sound/soc/codecs/src4xxx.c:280:3: error: variable 'd' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
                   default:
                   ^~~~~~~
   sound/soc/codecs/src4xxx.c:298:59: note: uninitialized use occurs here
                   ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_11, d);
                                                                           ^
   sound/soc/codecs/src4xxx.c:223:20: note: initialize the variable 'd' to silence this warning
           int val, pj, jd, d;
                             ^
                             = 0

If you really want to get rid of these warnings, you can use this default for the variables :

pj = 0x0;
jd=0xff;

Quoted 8 lines

d = 0xff;

It doesn't really make sense why we would choose to initialise these variables, but if you want to silence the preprocessor, then perhaps those values. Put a message that defaults are not known nor specified in the data sheet and these values are chosen to be unlikely matches of real world values - which ensures regmap updates later from an initial unknown chip state after refresh.

Okay, I could do something like this?

sound/soc/codecs/src4xxx.c +6 -1
index a8f143057b41..db4e280dd055 100644
@@ -280,9 +280,14 @@ static int src4xxx_hw_params(struct snd_pcm_substream *substream,
 		default:
 			/* don't error out here,
 			 * other parts of the chip are still functional
+			 * Dummy initialize variables to avoid
+			 * -Wsometimes-uninitialized from clang.
 			 */
 			dev_info(component->dev,
-				"Couldn't set the RCV PLL as this master clock rate is unknown\n");
+				"Couldn't set the RCV PLL as this master clock rate is unknown. Chosen regmap values may not match real world values.\n");
+			pj = 0x0;
+			jd = 0xff;
+			d = 0xff;
 			break;
 		}
 		ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_0F, pj);

These warnings need to be eliminated because Linus requires a clean build as much as possible [1] [2], which has been codified by CONFIG_WERROR, which is enabled by default with allmodconfig. As a result, clang allmodconfig builds are broken [3], which you might have already seen. If there is another way this could be written without potentially uninitialized variables, I am open to it.

[1]: https://lore.kernel.org/CAHk-=wg-mH-_GYpkhz_psjBWG6ZcjKnPo83fg7YMj_by+-LRTQ@mail.gmail.com/
[2]: https://lore.kernel.org/CAHk-=wi4wyvuXs0SUq-2x=XHxWmJ6jVKRD-WpE0kWgWiqVJNbg@mail.gmail.com/
[3]: https://lore.kernel.org/YwNMUlAmu%2FqCjuva@debian/

Quoted 1 line

All a bit messy really.

Indeed :/

Quoted 12 lines
Quoted 12 lines

According to the comment in the default case, other parts of the chip are still functional without these values so just return 0 in the default case to avoid using these variables uninitialized.

---
  sound/soc/codecs/src4xxx.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

sound/soc/codecs/src4xxx.c static int src4xxx_hw_params(struct snd_pcm_substream *substream, · line 286

Quoted 10 lines
sound/soc/codecs/src4xxx.c +1 -1
index a8f143057b41..cf45caa4bf7f 100644
@@ -283,7 +283,7 @@ static int src4xxx_hw_params(struct snd_pcm_substream *substream,
  			 */
  			dev_info(component->dev,
  				"Couldn't set the RCV PLL as this master clock rate is unknown\n");
-			break;
+			return 0;

Don't return here, the rest of the chip is still functional and probably in use. Print the dev_info and continue.

I'll send a v2 tomorrow morning (for me at least) if there are no other comments.

Cheers,
Nathan

#3 lore raw

That solution looks good.

Matt

Quoted 100 lines · Nathan Chancellor

Hi Matt,

Quoted 22 lines · Matt Flax

Hi Nathan,

Quoted 12 lines · Nathan Chancellor

Clang warns:

    sound/soc/codecs/src4xxx.c:280:3: error: variable 'd' is used uninitialized whenever switch default is taken [-Werror,-Wsometimes-uninitialized]
                    default:
                    ^~~~~~~
    sound/soc/codecs/src4xxx.c:298:59: note: uninitialized use occurs here
                    ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_11, d);
                                                                            ^
    sound/soc/codecs/src4xxx.c:223:20: note: initialize the variable 'd' to silence this warning
            int val, pj, jd, d;
                              ^
                              = 0

If you really want to get rid of these warnings, you can use this default for the variables :

pj = 0x0;
jd=0xff;

Quoted 8 lines

d = 0xff;

It doesn't really make sense why we would choose to initialise these variables, but if you want to silence the preprocessor, then perhaps those values. Put a message that defaults are not known nor specified in the data sheet and these values are chosen to be unlikely matches of real world values - which ensures regmap updates later from an initial unknown chip state after refresh.

Okay, I could do something like this?

sound/soc/codecs/src4xxx.c +6 -1
index a8f143057b41..db4e280dd055 100644
@@ -280,9 +280,14 @@ static int src4xxx_hw_params(struct snd_pcm_substream *substream,
  		default:
  			/* don't error out here,
  			 * other parts of the chip are still functional
+			 * Dummy initialize variables to avoid
+			 * -Wsometimes-uninitialized from clang.
  			 */
  			dev_info(component->dev,
-				"Couldn't set the RCV PLL as this master clock rate is unknown\n");
+				"Couldn't set the RCV PLL as this master clock rate is unknown. Chosen regmap values may not match real world values.\n");
+			pj = 0x0;
+			jd = 0xff;
+			d = 0xff;
  			break;
  		}
  		ret = regmap_write(src4xxx->regmap, SRC4XXX_RCV_PLL_0F, pj);

These warnings need to be eliminated because Linus requires a clean build as much as possible [1] [2], which has been codified by CONFIG_WERROR, which is enabled by default with allmodconfig. As a result, clang allmodconfig builds are broken [3], which you might have already seen. If there is another way this could be written without potentially uninitialized variables, I am open to it.

[1]: https://lore.kernel.org/CAHk-=wg-mH-_GYpkhz_psjBWG6ZcjKnPo83fg7YMj_by+-LRTQ@mail.gmail.com/
[2]: https://lore.kernel.org/CAHk-=wi4wyvuXs0SUq-2x=XHxWmJ6jVKRD-WpE0kWgWiqVJNbg@mail.gmail.com/
[3]: https://lore.kernel.org/YwNMUlAmu%2FqCjuva@debian/

Quoted 1 line

All a bit messy really.

Indeed :/

Quoted 12 lines
Quoted 12 lines

According to the comment in the default case, other parts of the chip are still functional without these values so just return 0 in the default case to avoid using these variables uninitialized.

---
   sound/soc/codecs/src4xxx.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

sound/soc/codecs/src4xxx.c static int src4xxx_hw_params(struct snd_pcm_substream *substream, · line 286

Quoted 10 lines
sound/soc/codecs/src4xxx.c +1 -1
index a8f143057b41..cf45caa4bf7f 100644
@@ -283,7 +283,7 @@ static int src4xxx_hw_params(struct snd_pcm_substream *substream,
   			 */
   			dev_info(component->dev,
   				"Couldn't set the RCV PLL as this master clock rate is unknown\n");
-			break;
+			return 0;

Don't return here, the rest of the chip is still functional and probably in use. Print the dev_info and continue.

I'll send a v2 tomorrow morning (for me at least) if there are no other comments.

Cheers,
Nathan