Skip to contentSkip to conversation

ASoC: cs-amp-lib: Replace offsetof() with struct_size()

PATCH In review Thorsten Blum 2 participants 2 messages Apr 14, 2025 – Apr 15, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ASoC: cs-amp-lib: Replace offsetof() with struct_size()

Use struct_size() to calculate the number of bytes to allocate and used by 'cirrus_amp_efi_data'. Compared to offsetof(), struct_size() provides additional compile-time checks (e.g., __must_be_array()).

---
Changes in v2:
- Add missing includes as suggested by Richard Fitzgerald (thanks!)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 sound/soc/codecs/cs-amp-lib-test.c | 4 ++--
 sound/soc/codecs/cs-amp-lib.c      | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
sound/soc/codecs/cs-amp-lib-test.c +2 -2
index 45626f99a417..d96d46eb5372 100644
@@ -12,6 +12,7 @@
 #include <linux/gpio/driver.h>
 #include <linux/list.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/platform_device.h>
 #include <linux/random.h>
 #include <sound/cs-amp-lib.h>
@@ -40,8 +41,7 @@ static void cs_amp_lib_test_init_dummy_cal_blob(struct kunit *test, int num_amps
 	unsigned int blob_size;
 	int i;
 
-	blob_size = offsetof(struct cirrus_amp_efi_data, data) +
-		    sizeof(struct cirrus_amp_cal_data) * num_amps;
+	blob_size = struct_size(priv->cal_blob, data, num_amps);
 
 	priv->cal_blob = kunit_kzalloc(test, blob_size, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_NULL(test, priv->cal_blob);
sound/soc/codecs/cs-amp-lib.c +2 -1
index c677868c5d5f..808e67c90f7c 100644
@@ -11,6 +11,7 @@
 #include <linux/efi.h>
 #include <linux/firmware/cirrus/cs_dsp.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/types.h>
 #include <sound/cs-amp-lib.h>
@@ -147,7 +148,7 @@ static struct cirrus_amp_efi_data *cs_amp_get_cal_efi_buffer(struct device *dev)
 	dev_dbg(dev, "Calibration: Size=%d, Amp Count=%d\n", efi_data->size, efi_data->count);
 
 	if ((efi_data->count > 128) ||
-	    offsetof(struct cirrus_amp_efi_data, data[efi_data->count]) > data_size) {
+	    struct_size(efi_data, data, efi_data->count) > data_size) {
 		dev_err(dev, "EFI cal variable truncated\n");
 		ret = -EOVERFLOW;
 		goto err;
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 5 lines · Thorsten Blum

Use struct_size() to calculate the number of bytes to allocate and used by 'cirrus_amp_efi_data'. Compared to offsetof(), struct_size() provides additional compile-time checks (e.g., __must_be_array()).

Applied to

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

Thanks!

[1/1] ASoC: cs-amp-lib: Replace offsetof() with struct_size()

      commit: 46e7ea05bf5d9fe4d05e173c824ae173c956cb5f

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