Skip to contentSkip to conversation

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

PATCH Superseded Thorsten Blum 2 participants 2 messages Apr 14, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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()).

---
Compile-tested only

---
 sound/soc/codecs/cs-amp-lib-test.c | 3 +--
 sound/soc/codecs/cs-amp-lib.c      | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
sound/soc/codecs/cs-amp-lib-test.c +1 -2
index 45626f99a417..2369fffb58f7 100644
@@ -40,8 +40,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 +1 -1
index c677868c5d5f..c2ce828a96ae 100644
@@ -147,7 +147,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 Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 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()).

---
Compile-tested only

---
  sound/soc/codecs/cs-amp-lib-test.c | 3 +--
  sound/soc/codecs/cs-amp-lib.c      | 2 +-
  2 files changed, 2 insertions(+), 3 deletions(-)
sound/soc/codecs/cs-amp-lib-test.c +1 -2
index 45626f99a417..2369fffb58f7 100644
@@ -40,8 +40,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 +0 -0
index c677868c5d5f..c2ce828a96ae 100644

sound/soc/codecs/cs-amp-lib.c static struct cirrus_amp_efi_data *cs_amp_get_cal_efi_buffer(struct device *dev) · line 153

Quoted 8 lines
sound/soc/codecs/cs-amp-lib.c +1 -1
@@ -147,7 +147,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;

It should include <linux/overflow.h>
Apart from that