[PATCH] ASoC: soc-core: check ops & auto_selectable_formats in snd_soc_dai_get_fmt() to prevent dereference error

hariconscious@gmail.com posted 1 patch 3 months, 1 week ago
sound/soc/soc-dai.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH] ASoC: soc-core: check ops & auto_selectable_formats in snd_soc_dai_get_fmt() to prevent dereference error
Posted by hariconscious@gmail.com 3 months, 1 week ago
From: HariKrishna Sagala <hariconscious@gmail.com>

Smatch reported an issue that "ops" could be null (see
line 174) where later "ops" is dereferenced to extract
the dai fmts, also auto_selectable_formats can also be
null.

Add a proper null check before accessing both the ptrs
to ensure a safe execution.

Signed-off-by: HariKrishna Sagala <hariconscious@gmail.com>
---
Hi,

Smatch flagged a null-pointer check in the function "snd_soc_dai_get_fmt". 
But the same validation is already performed in the earlier function call
"snd_soc_dai_get_fmt_max_priority" before calling into "snd_soc_dai_get_fmt".
While Smatch flags this, seeing it as a false positive, redundant null check 
added to make "snd_soc_dai_get_fmt" safe for independent reuse (if).

one way:
redundancy can also be removed in the line no.174

another way:
to have a null check considering independent functions

Please let me know.

Thanks.

 sound/soc/soc-dai.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/sound/soc/soc-dai.c b/sound/soc/soc-dai.c
index f231b4174b5f..a1e05307067d 100644
--- a/sound/soc/soc-dai.c
+++ b/sound/soc/soc-dai.c
@@ -177,8 +177,9 @@ u64 snd_soc_dai_get_fmt(const struct snd_soc_dai *dai, int priority)
 	if (max < until)
 		until = max;
 
-	for (i = 0; i < until; i++)
-		fmt |= ops->auto_selectable_formats[i];
+	if (ops && ops->auto_selectable_formats)
+		for (i = 0; i < until; i++)
+			fmt |= ops->auto_selectable_formats[i];
 
 	return fmt;
 }

base-commit: 6548d364a3e850326831799d7e3ea2d7bb97ba08
-- 
2.43.0
Re: [PATCH] ASoC: soc-core: check ops & auto_selectable_formats in snd_soc_dai_get_fmt() to prevent dereference error
Posted by Mark Brown 3 months, 1 week ago
On Thu, 30 Oct 2025 16:23:14 +0530, hariconscious@gmail.com wrote:
> Smatch reported an issue that "ops" could be null (see
> line 174) where later "ops" is dereferenced to extract
> the dai fmts, also auto_selectable_formats can also be
> null.
> 
> Add a proper null check before accessing both the ptrs
> to ensure a safe execution.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/1] ASoC: soc-core: check ops & auto_selectable_formats in snd_soc_dai_get_fmt() to prevent dereference error
      commit: e73b743bfe8a6ff4e05b5657d3f7586a17ac3ba0

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark