Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ASoC: Use __counted_by() annotation for snd_soc_pcm_runtime

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>

authored by

Takashi Iwai and committed by
Mark Brown
aaa5e1aa 42eb4731

+8 -8
+2 -1
include/sound/soc.h
··· 1209 1209 1210 1210 bool initialized; 1211 1211 1212 + /* CPU/Codec/Platform */ 1212 1213 int num_components; 1213 - struct snd_soc_component *components[]; /* CPU/Codec/Platform */ 1214 + struct snd_soc_component *components[] __counted_by(num_components); 1214 1215 }; 1215 1216 1216 1217 /* see soc_new_pcm_runtime() */
+6 -7
sound/soc/soc-core.c
··· 326 326 } 327 327 328 328 /* see for_each_rtd_components */ 329 - rtd->components[rtd->num_components] = component; 330 - rtd->num_components++; 329 + rtd->num_components++; // increment flex array count at first 330 + rtd->components[rtd->num_components - 1] = component; 331 331 332 332 return 0; 333 333 } ··· 494 494 struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) 495 495 { 496 496 struct snd_soc_pcm_runtime *rtd; 497 - struct snd_soc_component *component; 498 497 struct device *dev; 499 498 int ret; 500 499 int stream; ··· 520 521 * for rtd 521 522 */ 522 523 rtd = devm_kzalloc(dev, 523 - sizeof(*rtd) + 524 - sizeof(component) * (dai_link->num_cpus + 525 - dai_link->num_codecs + 526 - dai_link->num_platforms), 524 + struct_size(rtd, components, 525 + dai_link->num_cpus + 526 + dai_link->num_codecs + 527 + dai_link->num_platforms), 527 528 GFP_KERNEL); 528 529 if (!rtd) { 529 530 device_unregister(dev);