Re: [PATCH 2/2] ASoC: Add BUG() assertion if max98095_get_bq_channelreturns -EINVAL

From: Dave Young
Date: Wed Sep 28 2011 - 22:06:06 EST


On Thu, Sep 29, 2011 at 10:01 AM, Ryan Mallon <rmallon@xxxxxxxxx> wrote:
> On 29/09/11 11:59, Dave Young wrote:
>
>> On Thu, Sep 29, 2011 at 9:52 AM, Ryan Mallon <rmallon@xxxxxxxxx> wrote:
>>> On 29/09/11 11:35, Dave Young wrote:
>>>
>>>> On Thu, Sep 29, 2011 at 7:19 AM, Ryan Mallon <rmallon@xxxxxxxxx> wrote:
>>>>> On 29/09/11 00:02, Axel Lin wrote:
>>>>>> The callers use the return value of max98095_get_bq_channel as array index to
>>>>>> access max98095->dai[] array.
>>>>>> Add BUG() assertion for out of bound access of max98095->dai[] array.
>>>>>
>>>>> Same here, fix the problem in the callers.
>>>>>
>>>>> ----
>>>>> Check the return value of max98095_get_bq_channel in the callers and
>>>>> propagate any errors up. Remove the BUG_ON(channel > 1) since
>>>>> max98095_get_bq_channel never returns a value larger than 1.
>>>>>
>>>>> Signed-off-by: Ryan Mallon <rmallon@xxxxxxxxx>
>>>>> ---
>>>>>
>>>>> diff --git a/sound/soc/codecs/max98095.c b/sound/soc/codecs/max98095.c
>>>>> index 668434d..55eccea 100644
>>>>> --- a/sound/soc/codecs/max98095.c
>>>>> +++ b/sound/soc/codecs/max98095.c
>>>>> @@ -2014,7 +2014,8 @@ static int max98095_put_bq_enum(struct snd_kcontrol *kcontrol,
>>>>> Â Â Â Âint fs, best, best_val, i;
>>>>> Â Â Â Âint regmask, regsave;
>>>>>
>>>>> - Â Â Â BUG_ON(channel > 1);
>>>>> + Â Â Â if (channel < 0)
>>>>> + Â Â Â Â Â Â Â return channel;
>>>>
>>>> If use BUG() happens in Âmax98095_get_bq_channel, it will not return here?
>>>
>>>
>>> Not quite sure what you mean?
>>
>> I means if Axel Lin's patch applied, and CONFIG_BUG is on, it will
>> panic firstly the if condition will be never entered.
>
> My patch is a replacement for Axel's patch, not on top of it. For Axel's
> patch it would panic if channel was less than zero if CONFIG_BUG was
> enabled, but would still have undefined behaviour if CONFIG_BUG was not
> enabled.

So that's good, thanks

>
> ~Ryan
>
>>>
>>> If CONFIG_BUG was not enabled for the original version, then it would
>>> not return at the BUG_ON and would either crash or cause odd behaviour
>>> if it tried to index channel as -1.
>>>
>>> My patch is removing the BUG_ON and replacing it with a proper check and
>>> return. It doesn't need to check > 1 since max98095_get_bq_channel never
>>> returns that.
>>>
>>> My understanding is that device drivers, in general, should not call
>>> BUG. BUG is for unrecoverable errors which leave the kernel in some
>>> unstable state. Here we can just return an error code.
>>
>> Agree
>>
>>>
>>> ~Ryan
>>>
>>>>
>>>>>
>>>>> Â Â Â Âif (!pdata || !max98095->bq_textcnt)
>>>>> Â Â Â Â Â Â Â Âreturn 0;
>>>>> @@ -2069,6 +2070,9 @@ static int max98095_get_bq_enum(struct snd_kcontrol *kcontrol,
>>>>> Â Â Â Âint channel = max98095_get_bq_channel(kcontrol->id.name);
>>>>> Â Â Â Âstruct max98095_cdata *cdata;
>>>>>
>>>>> + Â Â Â if (channel < 0)
>>>>> + Â Â Â Â Â Â Â return channel;
>>>>> +
>>>>> Â Â Â Âcdata = &max98095->dai[channel];
>>>>> Â Â Â Âucontrol->value.enumerated.item[0] = cdata->bq_sel;
>>>>>
>>>>>
>>>>> --
>>>>> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>>>>> the body of a message to majordomo@xxxxxxxxxxxxxxx
>>>>> More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html
>>>>> Please read the FAQ at Âhttp://www.tux.org/lkml/
>>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>>
>>
>
>
>



--
Regards
Dave
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/