Re: [alsa-devel] [PATCH 1/2] ASoC: codec: wm9860: avoid maybe-uninitialized warning

From: Arnd Bergmann
Date: Mon Apr 24 2017 - 11:28:06 EST


On Mon, Apr 24, 2017 at 3:15 PM, Daniel Baluta <daniel.baluta@xxxxxxxxx> wrote:
> On Fri, Apr 21, 2017 at 5:46 PM, Arnd Bergmann <arnd@xxxxxxxx> wrote:
>> On Fri, Apr 21, 2017 at 3:07 PM, Daniel Baluta <daniel.baluta@xxxxxxx> wrote:
>>> The new PLL configuration code triggers a harmless warning:
>>>
>>> sound/soc/codecs/wm8960.c: In function 'wm8960_configure_clocking':
>>> sound/soc/codecs/wm8960.c:735:3: error: 'best_freq_out' may be used
>>> uninitialized in this function [-Werror=maybe-uninitialized]
>>> wm8960_set_pll(codec, freq_in, best_freq_out);
>>> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> sound/soc/codecs/wm8960.c:699:12: note: 'best_freq_out' was declared
>>> here
>>>
>>> Fixes: 84fdc00d519f ("ASoC: codec: wm9860: Refactor PLL out freq search")
>>> Fixes: 303e8954af8d ("ASoC: codec: wm8960: Stop when a matching PLL freq is found")
>>> Suggested-by: Arnd Bergmann <arnd@xxxxxxxx>
>>> Signed-off-by: Daniel Baluta <daniel.baluta@xxxxxxx>
>>> ---
>>> Arnd,
>>>
>>> I agree that your code was more both humans and gcc anyhow
>>> for consistency with wm8960_configure_sysclk function I preferred
>>> to keep the "if(..) break" statements.
>>
>> How about changing both functions the same way then?
>
> I've tried but I couldn't find any solution. For clarity here is how
> the code actually looks like.
>
> The git diff is a little bit misleading. Here is how wm8960_configure_pll code
> looks like:
>
> https://pastebin.com/naGdVNQz
>
> static
> int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in,
> Â Â Â int *sysclk_idx, int *dac_idx, int *bclk_idx)
> {
> Â struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec);
> Â int sysclk, bclk, lrclk, freq_out;
> Â int diff, closest, best_freq_out;
> Â int i, j, k;
>
> Â bclk = wm8960->bclk;
> Â lrclk = wm8960->lrclk;
> Â closest = freq_in;
>
> Â best_freq_out = -EINVAL;
> Â *sysclk_idx = *dac_idx = *bclk_idx = -1;
>
> Â for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) {
> Â Â if (sysclk_divs[i] == -1)
> Â Â Â continue;
> Â Â for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) {
> Â Â Â sysclk = lrclk * dac_divs[j];
> Â Â Â freq_out = sysclk * sysclk_divs[i];
>
> Â Â Â for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) {
> Â Â Â Â if (!is_pll_freq_available(freq_in, freq_out))
> Â Â Â Â Â continue;
>
> Â Â Â Â diff = sysclk - bclk * bclk_divs[k] / 10;
> Â Â Â Â if (diff == 0) {
> Â Â Â Â Â *sysclk_idx = i;
> Â Â Â Â Â *dac_idx = j;
> Â Â Â Â Â *bclk_idx = k;
> Â Â Â Â Â best_freq_out = freq_out;
> Â Â Â Â Â break;
> Â Â Â Â }
> Â Â Â Â if (diff > 0 && closest > diff) {
> Â Â Â Â Â *sysclk_idx = i;
> Â Â Â Â Â *dac_idx = j;
> Â Â Â Â Â *bclk_idx = k;
> Â Â Â Â Â closest = diff;
> Â Â Â Â Â best_freq_out = freq_out;
> Â Â Â Â }
> Â Â Â }
> Â Â Â if (k != ARRAY_SIZE(bclk_divs))
> Â Â Â Â break;
> Â Â }
> Â Â if (j != ARRAY_SIZE(dac_divs))
> Â Â Â break;
> Â }
>
> Â return best_freq_out;
> }
>
> In my opinion this is a compiler false positive. Any clue on how to rework this
> would be welcomed :). I couldn't find any decent solution.

Actually I think in this case the compiler is supposed to warn if
best_freq_out is not initialized, as we would never set it
in case is_pll_freq_available() returns false for all inputs or
sysclk_divs[] is -1 for all fields.

I'd leave the initialization then, and only replace the breaks
with a goto (not tested):

> Â for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) {
> Â Â if (sysclk_divs[i] == -1)
> Â Â Â continue;
> Â Â for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) {
> Â Â Â sysclk = lrclk * dac_divs[j];
> Â Â Â freq_out = sysclk * sysclk_divs[i];
>
> Â Â Â for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) {
> Â Â Â Â if (!is_pll_freq_available(freq_in, freq_out))
> Â Â Â Â Â continue;
>
> Â Â Â Â diff = sysclk - bclk * bclk_divs[k] / 10;
> Â Â Â Â if (diff == 0) {
> Â Â Â Â Â *sysclk_idx = i;
> Â Â Â Â Â *dac_idx = j;
> Â Â Â Â Â *bclk_idx = k;
> Â Â Â Â Â best_freq_out = freq_out;
> Â Â Â Â Â goto out;
> Â Â Â Â }
> Â Â Â Â if (diff > 0 && closest > diff) {
> Â Â Â Â Â *sysclk_idx = i;
> Â Â Â Â Â *dac_idx = j;
> Â Â Â Â Â *bclk_idx = k;
> Â Â Â Â Â closest = diff;
> Â Â Â Â Â best_freq_out = freq_out;
> Â Â Â Â }
> Â Â Â }
> Â Â }
> Â }
>out:
> Â return best_freq_out;
> }


Arnd