[PATCH] ASoC: codecs: Use sizeof(struct) for aw_container allocation

wangdich9700@163.com posted 1 patch 6 days, 22 hours ago
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(-)
[PATCH] ASoC: codecs: Use sizeof(struct) for aw_container allocation
Posted by wangdich9700@163.com 6 days, 22 hours ago
From: wangdicheng <wangdicheng@kylinos.cn>

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

While sizeof(int) and sizeof(struct aw_container) may have the same
size on most platforms (both are typically 4 bytes), using the proper
struct size is the correct approach according to kernel coding style
for structures with flexible array members.

The standard pattern for allocating such structures is:
  sizeof(struct) + flexible_array_size

Fixes: 62fc25fbab5f ("ASoC: codecs: Add i2c and codec registration for aw88395 and their associated operation functions")
Signed-off-by: wangdicheng <wangdicheng@kylinos.cn>
---
 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..01e8610a8009 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,
+				       sizeof(struct aw_container) + 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..7fcb3d7ec3fb 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,
+				       sizeof(struct aw_container) + 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..b4ddf9ac9ca0 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,
+					sizeof(struct aw_container) + cont->size,
+					GFP_KERNEL);
 	if (!aw88395->aw_cfg) {
 		release_firmware(cont);
 		return -ENOMEM;
-- 
2.25.1
Re: [PATCH] ASoC: codecs: Use sizeof(struct) for aw_container allocation
Posted by Mark Brown 6 days, 11 hours ago
On Mon, Jun 01, 2026 at 03:25:53PM +0800, wangdich9700@163.com wrote:

> -	aw88081->aw_cfg = devm_kzalloc(aw88081->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
> +	aw88081->aw_cfg = devm_kzalloc(aw88081->aw_pa->dev,
> +				       sizeof(struct aw_container) + cont->size,
> +				       GFP_KERNEL);

We have a bunch of helper macros like struct_size() for this stuff.