Re: [PATCH] ASoC: core: add snd_BUG_ON() check in devm_snd_soc_register_card()

Cezary Rojewski <[email protected]> Mon, 3 Aug 2026 12:50:10 +0200
Newsgroups gmane.linux.sound,gmane.linux.kernel
Message-ID <[email protected]>
On 8/3/2026 12:27 PM, [email protected] wrote:
> From: bui duc phuc <[email protected]>
> 
> card is dereferenced immediately, so passing NULL results in a NULL
> pointer dereference. A NULL dev is silently accepted, defeating the
> documented automatic-unregistration semantics of this devm variant.
> 
> Add snd_BUG_ON() checks on both arguments to catch API misuse early.
> 
> Signed-off-by: bui duc phuc <[email protected]>
> ---
>   sound/soc/soc-devres.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/sound/soc/soc-devres.c b/sound/soc/soc-devres.c
> index 718165ba84ac..3e946196022a 100644
> --- a/sound/soc/soc-devres.c
> +++ b/sound/soc/soc-devres.c
> @@ -59,6 +59,9 @@ EXPORT_SYMBOL_GPL(devm_snd_soc_register_component);
>    */
>   int devm_snd_soc_register_card(struct device *dev, struct snd_soc_card *card)
>   {
> +	if (snd_BUG_ON(!dev || !card))
> +		return -EINVAL;
> +
>   	card->devres_dev = dev;
>   	return snd_soc_register_card(card);
>   }

Not sure whether we need such defenses. If we are to follow such 
approach, many ASoC-driver APIs require BUG_ON/WARN_ON() update.

register_card() is typically called as a last step, once 'card' is 
already allocated. At the same time, the register_card() typically 
occurs in probe() context of given device so we're always certain the 
'dev' is not NULL.

TLDR: this is not a UAPI, (obvious) NULL-checks can be skipped.