Skip to content

Commit 60aca6f

Browse files
JiangJiasgregkh
authored andcommitted
staging: greybus: audio: Check null pointer
[ Upstream commit 2e81948 ] As the possible alloc failure of devm_kcalloc(), it could return null pointer. Therefore, 'strings' should be checked and return NULL if alloc fails to prevent the dereference of the NULL pointer. Also, the caller should also deal with the return value of the gb_generate_enum_strings() and return -ENOMEM if returns NULL. Moreover, because the memory allocated with devm_kzalloc() will be freed automatically when the last reference to the device is dropped, the 'gbe' in gbaudio_tplg_create_enum_kctl() and gbaudio_tplg_create_enum_ctl() do not need to free manually. But the 'control' in gbaudio_tplg_create_widget() and gbaudio_tplg_process_kcontrols() has a specially error handle to cleanup. So it should be better to cleanup 'control' when fails. Fixes: e65579e ("greybus: audio: topology: Enable enumerated control support") Reviewed-by: Alex Elder <elder@linaro.org> Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn> Link: https://lore.kernel.org/r/20220104150628.1987906-1-jiasheng@iscas.ac.cn Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a1068bf commit 60aca6f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

‎drivers/staging/greybus/audio_topology.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ static const char **gb_generate_enum_strings(struct gbaudio_module_info *gb,
147147

148148
items = le32_to_cpu(gbenum->items);
149149
strings = devm_kcalloc(gb->dev, items, sizeof(char *), GFP_KERNEL);
150+
if (!strings)
151+
return NULL;
152+
150153
data = gbenum->names;
151154

152155
for (i = 0; i < items; i++) {
@@ -655,6 +658,8 @@ static int gbaudio_tplg_create_enum_kctl(struct gbaudio_module_info *gb,
655658
/* since count=1, and reg is dummy */
656659
gbe->items = le32_to_cpu(gb_enum->items);
657660
gbe->texts = gb_generate_enum_strings(gb, gb_enum);
661+
if (!gbe->texts)
662+
return -ENOMEM;
658663

659664
/* debug enum info */
660665
dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
@@ -862,6 +867,8 @@ static int gbaudio_tplg_create_enum_ctl(struct gbaudio_module_info *gb,
862867
/* since count=1, and reg is dummy */
863868
gbe->items = le32_to_cpu(gb_enum->items);
864869
gbe->texts = gb_generate_enum_strings(gb, gb_enum);
870+
if (!gbe->texts)
871+
return -ENOMEM;
865872

866873
/* debug enum info */
867874
dev_dbg(gb->dev, "Max:%d, name_length:%d\n", gbe->items,
@@ -1072,6 +1079,10 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
10721079
csize += le16_to_cpu(gbenum->names_length);
10731080
control->texts = (const char * const *)
10741081
gb_generate_enum_strings(module, gbenum);
1082+
if (!control->texts) {
1083+
ret = -ENOMEM;
1084+
goto error;
1085+
}
10751086
control->items = le32_to_cpu(gbenum->items);
10761087
} else {
10771088
csize = sizeof(struct gb_audio_control);
@@ -1181,6 +1192,10 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
11811192
csize += le16_to_cpu(gbenum->names_length);
11821193
control->texts = (const char * const *)
11831194
gb_generate_enum_strings(module, gbenum);
1195+
if (!control->texts) {
1196+
ret = -ENOMEM;
1197+
goto error;
1198+
}
11841199
control->items = le32_to_cpu(gbenum->items);
11851200
} else {
11861201
csize = sizeof(struct gb_audio_control);

0 commit comments

Comments
 (0)