ASoC: codecs: Use struct_size() for aw_container allocation
PATCH v2 1/1 ASoC: codecs: Use struct_size() 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 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.
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(-)
sound/soc/codecs/aw88081.c +3 -1
@@ -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 ;
sound/soc/codecs/aw88261.c +3 -1
@@ -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 ;
sound/soc/codecs/aw88395/aw88395.c +3 -1
@@ -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 ;
Signature · 1 line
2.25.1
Mark Brown
maintainerQuoted 6 lines · On Tue, Jun 02, 2026 at 09:37:01AM +0800, [email protected] wrote:
From: wangdicheng <[email protected]>
The aw88395 driver and its variants (aw88081, aw88261) use sizeof(int) when allocating memory for struct
aw_container. This should usestruct_size()helper macro instead for proper type safety and portability.
Actually someone else already sent this change, it's in my current tree.