public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ASoC: cs-amp-lib: Replace offsetof() with struct_size()
@ 2025-04-14  6:59 Thorsten Blum
  2025-04-14 10:00 ` Richard Fitzgerald
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2025-04-14  6:59 UTC (permalink / raw)
  To: David Rhodes, Richard Fitzgerald, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: Thorsten Blum, linux-sound, patches, linux-kernel

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

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
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(-)

diff --git a/sound/soc/codecs/cs-amp-lib-test.c b/sound/soc/codecs/cs-amp-lib-test.c
index 45626f99a417..2369fffb58f7 100644
--- a/sound/soc/codecs/cs-amp-lib-test.c
+++ b/sound/soc/codecs/cs-amp-lib-test.c
@@ -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);
diff --git a/sound/soc/codecs/cs-amp-lib.c b/sound/soc/codecs/cs-amp-lib.c
index c677868c5d5f..c2ce828a96ae 100644
--- a/sound/soc/codecs/cs-amp-lib.c
+++ b/sound/soc/codecs/cs-amp-lib.c
@@ -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;
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ASoC: cs-amp-lib: Replace offsetof() with struct_size()
  2025-04-14  6:59 [PATCH] ASoC: cs-amp-lib: Replace offsetof() with struct_size() Thorsten Blum
@ 2025-04-14 10:00 ` Richard Fitzgerald
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Fitzgerald @ 2025-04-14 10:00 UTC (permalink / raw)
  To: Thorsten Blum, David Rhodes, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai
  Cc: linux-sound, patches, linux-kernel

On 14/04/2025 7:59 am, Thorsten Blum wrote:
> 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()).
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
> 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(-)
> 
> diff --git a/sound/soc/codecs/cs-amp-lib-test.c b/sound/soc/codecs/cs-amp-lib-test.c
> index 45626f99a417..2369fffb58f7 100644
> --- a/sound/soc/codecs/cs-amp-lib-test.c
> +++ b/sound/soc/codecs/cs-amp-lib-test.c
> @@ -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);
> diff --git a/sound/soc/codecs/cs-amp-lib.c b/sound/soc/codecs/cs-amp-lib.c
> index c677868c5d5f..c2ce828a96ae 100644
> --- a/sound/soc/codecs/cs-amp-lib.c
> +++ b/sound/soc/codecs/cs-amp-lib.c
> @@ -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
Reviewed-by: Richard Fitzgerald <rf@opensource.cirrus.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-04-14 10:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-14  6:59 [PATCH] ASoC: cs-amp-lib: Replace offsetof() with struct_size() Thorsten Blum
2025-04-14 10:00 ` Richard Fitzgerald

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox