]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ASoC: Use __counted_by() annotation for snd_soc_pcm_runtime
authorTakashi Iwai <tiwai@suse.de>
Fri, 26 Jul 2024 15:52:36 +0000 (17:52 +0200)
committerMark Brown <broonie@kernel.org>
Mon, 29 Jul 2024 00:18:42 +0000 (01:18 +0100)
The struct snd_soc_pcm_runtime has a flex array of snd_soc_component
objects at its end, and the size is kept in num_components field.
We can add __counted_by() annotation for compiler's assistance to
catch array overflows.

A slight additional change is the assignment of rtd->components[];
the array counter has to be incremented at first for avoiding
false-positive reports from compilers.

Also, the allocation size of snd_soc_pcm_runtime is cleaned up with
the standard struct_size() helper, too.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20240726155237.21961-1-tiwai@suse.de
Signed-off-by: Mark Brown <broonie@kernel.org>
include/sound/soc.h
sound/soc/soc-core.c

index a8e66bbf932b333447abce2bd053cca2436c8f57..e844f6afc5b550d84b3f6d0b4900d6c9c42a5047 100644 (file)
@@ -1209,8 +1209,9 @@ struct snd_soc_pcm_runtime {
 
        bool initialized;
 
+       /* CPU/Codec/Platform */
        int num_components;
-       struct snd_soc_component *components[]; /* CPU/Codec/Platform */
+       struct snd_soc_component *components[] __counted_by(num_components);
 };
 
 /* see soc_new_pcm_runtime()  */
index 724fe1f033b5505be197c1008ce7a310c0f27d54..80bacea6bb904e4d6924cea3720b5f07886bd9af 100644 (file)
@@ -326,8 +326,8 @@ static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
        }
 
        /* see for_each_rtd_components */
-       rtd->components[rtd->num_components] = component;
-       rtd->num_components++;
+       rtd->num_components++; // increment flex array count at first
+       rtd->components[rtd->num_components - 1] = component;
 
        return 0;
 }
@@ -494,7 +494,6 @@ static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
        struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
 {
        struct snd_soc_pcm_runtime *rtd;
-       struct snd_soc_component *component;
        struct device *dev;
        int ret;
        int stream;
@@ -521,10 +520,10 @@ static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
         * for rtd
         */
        rtd = devm_kzalloc(dev,
-                          sizeof(*rtd) +
-                          sizeof(component) * (dai_link->num_cpus +
-                                                dai_link->num_codecs +
-                                                dai_link->num_platforms),
+                          struct_size(rtd, components,
+                                      dai_link->num_cpus +
+                                      dai_link->num_codecs +
+                                      dai_link->num_platforms),
                           GFP_KERNEL);
        if (!rtd) {
                device_unregister(dev);