Skip to content

Commit 36cf165

Browse files
nehebbroonie
authored andcommitted
ASoC: aw88395: use struct_size() and __counted_by() for aw_container
The firmware container allocations passed cont->size + sizeof(int) to kzalloc(), which was wrong: the struct contains an int len followed by a u8 data[] flexible array. It ended up being the same as the struct's size is only the int member but still wrong. Use the modern struct_size helper for this. Add __counted_by for extra runtime analysis. Assisted-by: Claude:Opus-4.7 Signed-off-by: Rosen Penev <rosenp@gmail.com> Link: https://patch.msgid.link/20260523011749.101555-1-rosenp@gmail.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 3a12998 commit 36cf165

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

sound/soc/codecs/aw88081.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ static int aw88081_dev_init(struct aw88081 *aw88081, struct aw_container *aw_cfg
11371137
static int aw88081_request_firmware_file(struct aw88081 *aw88081)
11381138
{
11391139
const struct firmware *cont = NULL;
1140+
struct aw_container *aw_cfg;
11401141
int ret;
11411142

11421143
aw88081->aw_pa->fw_status = AW88081_DEV_FW_FAILED;
@@ -1148,13 +1149,16 @@ static int aw88081_request_firmware_file(struct aw88081 *aw88081)
11481149
dev_dbg(aw88081->aw_pa->dev, "loaded %s - size: %zu\n",
11491150
AW88081_ACF_FILE, cont ? cont->size : 0);
11501151

1151-
aw88081->aw_cfg = devm_kzalloc(aw88081->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1152-
if (!aw88081->aw_cfg) {
1152+
aw_cfg = devm_kzalloc(aw88081->aw_pa->dev, struct_size(aw_cfg, data, cont->size), GFP_KERNEL);
1153+
if (!aw_cfg) {
11531154
release_firmware(cont);
11541155
return -ENOMEM;
11551156
}
1156-
aw88081->aw_cfg->len = (int)cont->size;
1157-
memcpy(aw88081->aw_cfg->data, cont->data, cont->size);
1157+
aw_cfg->len = (int)cont->size;
1158+
memcpy(aw_cfg->data, cont->data, cont->size);
1159+
1160+
aw88081->aw_cfg = aw_cfg;
1161+
11581162
release_firmware(cont);
11591163

11601164
ret = aw88395_dev_load_acf_check(aw88081->aw_pa, aw88081->aw_cfg);

sound/soc/codecs/aw88261.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ static int aw88261_dev_init(struct aw88261 *aw88261, struct aw_container *aw_cfg
10941094
static int aw88261_request_firmware_file(struct aw88261 *aw88261)
10951095
{
10961096
const struct firmware *cont = NULL;
1097+
struct aw_container *aw_cfg;
10971098
const char *fw_name;
10981099
int ret;
10991100

@@ -1111,15 +1112,17 @@ static int aw88261_request_firmware_file(struct aw88261 *aw88261)
11111112
dev_info(aw88261->aw_pa->dev, "loaded %s - size: %zu\n",
11121113
fw_name, cont ? cont->size : 0);
11131114

1114-
aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1115-
if (!aw88261->aw_cfg) {
1115+
aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, struct_size(aw_cfg, data, cont->size), GFP_KERNEL);
1116+
if (!aw_cfg) {
11161117
release_firmware(cont);
11171118
return -ENOMEM;
11181119
}
1119-
aw88261->aw_cfg->len = (int)cont->size;
1120-
memcpy(aw88261->aw_cfg->data, cont->data, cont->size);
1120+
aw_cfg->len = (int)cont->size;
1121+
memcpy(aw_cfg->data, cont->data, cont->size);
11211122
release_firmware(cont);
11221123

1124+
aw88261->aw_cfg = aw_cfg;
1125+
11231126
ret = aw88395_dev_load_acf_check(aw88261->aw_pa, aw88261->aw_cfg);
11241127
if (ret) {
11251128
dev_err(aw88261->aw_pa->dev, "load [%s] failed !", fw_name);

sound/soc/codecs/aw88395/aw88395.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ static void aw88395_hw_reset(struct aw88395 *aw88395)
462462
static int aw88395_request_firmware_file(struct aw88395 *aw88395)
463463
{
464464
const struct firmware *cont = NULL;
465+
struct aw_container *aw_cfg;
465466
int ret;
466467

467468
aw88395->aw_pa->fw_status = AW88395_DEV_FW_FAILED;
@@ -475,15 +476,17 @@ static int aw88395_request_firmware_file(struct aw88395 *aw88395)
475476
dev_info(aw88395->aw_pa->dev, "loaded %s - size: %zu\n",
476477
AW88395_ACF_FILE, cont ? cont->size : 0);
477478

478-
aw88395->aw_cfg = devm_kzalloc(aw88395->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
479-
if (!aw88395->aw_cfg) {
479+
aw_cfg = devm_kzalloc(aw88395->aw_pa->dev, struct_size(aw_cfg, data, cont->size), GFP_KERNEL);
480+
if (!aw_cfg) {
480481
release_firmware(cont);
481482
return -ENOMEM;
482483
}
483-
aw88395->aw_cfg->len = (int)cont->size;
484-
memcpy(aw88395->aw_cfg->data, cont->data, cont->size);
484+
aw_cfg->len = (int)cont->size;
485+
memcpy(aw_cfg->data, cont->data, cont->size);
485486
release_firmware(cont);
486487

488+
aw88395->aw_cfg = aw_cfg;
489+
487490
ret = aw88395_dev_load_acf_check(aw88395->aw_pa, aw88395->aw_cfg);
488491
if (ret < 0) {
489492
dev_err(aw88395->aw_pa->dev, "Load [%s] failed ....!", AW88395_ACF_FILE);

sound/soc/codecs/aw88395/aw88395_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct aw_cali_desc {
152152

153153
struct aw_container {
154154
int len;
155-
u8 data[];
155+
u8 data[] __counted_by(len);
156156
};
157157

158158
struct aw_device {

0 commit comments

Comments
 (0)