[PATCH v2] ASoC: codecs: Use struct_size() for aw_container allocation

From: wangdich9700

Date: Mon Jun 01 2026 - 21:37:54 EST


From: wangdicheng <wangdicheng@xxxxxxxxxx>

The aw88395 driver and its variants (aw88081, aw88261) use sizeof(int)
when allocating memory for struct aw_container. This should use
struct_size() helper macro instead for proper type safety and
portability.

The struct_size() macro is the standard kernel way to calculate the
size of a structure with a flexible array member, providing better
type checking and avoiding potential issues with manual calculations.

Fixes: 62fc25fbab5f ("ASoC: codecs: Add i2c and codec registration for aw88395 and their associated operation functions")
Signed-off-by: wangdicheng <wangdicheng@xxxxxxxxxx>

v1->v2: Use struct_size() helper macro instead of sizeof(struct) as
suggested by maintainers.
---
sound/soc/codecs/aw88081.c | 4 +++-
sound/soc/codecs/aw88261.c | 4 +++-
sound/soc/codecs/aw88395/aw88395.c | 4 +++-
3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/sound/soc/codecs/aw88081.c b/sound/soc/codecs/aw88081.c
index 8c5bb3ea0227..40f1ed6f8a80 100644
--- a/sound/soc/codecs/aw88081.c
+++ b/sound/soc/codecs/aw88081.c
@@ -1148,7 +1148,9 @@ static int aw88081_request_firmware_file(struct aw88081 *aw88081)
dev_dbg(aw88081->aw_pa->dev, "loaded %s - size: %zu\n",
AW88081_ACF_FILE, cont ? cont->size : 0);

- aw88081->aw_cfg = devm_kzalloc(aw88081->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
+ aw88081->aw_cfg = devm_kzalloc(aw88081->aw_pa->dev,
+ struct_size(aw88081->aw_cfg, data, cont->size),
+ GFP_KERNEL);
if (!aw88081->aw_cfg) {
release_firmware(cont);
return -ENOMEM;
diff --git a/sound/soc/codecs/aw88261.c b/sound/soc/codecs/aw88261.c
index a6805d5405cd..4094ce8b7038 100644
--- a/sound/soc/codecs/aw88261.c
+++ b/sound/soc/codecs/aw88261.c
@@ -1111,7 +1111,9 @@ static int aw88261_request_firmware_file(struct aw88261 *aw88261)
dev_info(aw88261->aw_pa->dev, "loaded %s - size: %zu\n",
fw_name, cont ? cont->size : 0);

- aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
+ aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev,
+ struct_size(aw88261->aw_cfg, data, cont->size),
+ GFP_KERNEL);
if (!aw88261->aw_cfg) {
release_firmware(cont);
return -ENOMEM;
diff --git a/sound/soc/codecs/aw88395/aw88395.c b/sound/soc/codecs/aw88395/aw88395.c
index dd09bac652f7..1988ff62db3f 100644
--- a/sound/soc/codecs/aw88395/aw88395.c
+++ b/sound/soc/codecs/aw88395/aw88395.c
@@ -475,7 +475,9 @@ static int aw88395_request_firmware_file(struct aw88395 *aw88395)
dev_info(aw88395->aw_pa->dev, "loaded %s - size: %zu\n",
AW88395_ACF_FILE, cont ? cont->size : 0);

- aw88395->aw_cfg = devm_kzalloc(aw88395->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
+ aw88395->aw_cfg = devm_kzalloc(aw88395->aw_pa->dev,
+ struct_size(aw88395->aw_cfg, data, cont->size),
+ GFP_KERNEL);
if (!aw88395->aw_cfg) {
release_firmware(cont);
return -ENOMEM;
--
2.25.1