Skip to contentSkip to conversation

ASoC: codecs: Use sizeof(struct) for aw_container allocation

PATCH Superseded [email protected] 2 participants 2 messages Jun 1

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: codecs: Use sizeof(struct) for aw_container allocation

From: wangdicheng <[email protected]>

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")
---
 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(-)
sound/soc/codecs/aw88081.c +3 -1
index 8c5bb3ea0227..01e8610a8009 100644
@@ -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;
sound/soc/codecs/aw88261.c +3 -1
index a6805d5405cd..7fcb3d7ec3fb 100644
@@ -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;
sound/soc/codecs/aw88395/aw88395.c +3 -1
index dd09bac652f7..b4ddf9ac9ca0 100644
@@ -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;
Signature · 1 line
2.25.1
maintainer
#1 lore raw

sound/soc/codecs/aw88081.c static int aw88081_request_firmware_file(struct aw88081 *aw88081) · line 1153

Quoted 4 lines · On Mon, Jun 01, 2026 at 03:25:53PM +0800, [email protected] wrote:
sound/soc/codecs/aw88081.c +3 -1
-	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.