Skip to content

Commit 2c7c857

Browse files
KunWuChanij-intel
authored andcommitted
platform/mellanox: Add null pointer checks for devm_kasprintf()
devm_kasprintf() returns a pointer to dynamically allocated memory which can be NULL upon failure. Compile-tested only. Fixes: 1a218d3 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver") Suggested-by: Ilpo Järvinen <[email protected]> Suggested-by: Vadim Pasternak <[email protected]> Signed-off-by: Kunwu Chan <[email protected]> Reviewed-by: Vadim Pasternak <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ij: split the change into two] Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: Ilpo Järvinen <[email protected]>
1 parent d4eef75 commit 2c7c857

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

drivers/platform/mellanox/mlxbf-pmc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
17711771
attr->dev_attr.show = mlxbf_pmc_event_list_show;
17721772
attr->nr = blk_num;
17731773
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, "event_list");
1774+
if (!attr->dev_attr.attr.name)
1775+
return -ENOMEM;
17741776
pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
17751777
attr = NULL;
17761778

@@ -1784,6 +1786,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
17841786
attr->nr = blk_num;
17851787
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
17861788
"enable");
1789+
if (!attr->dev_attr.attr.name)
1790+
return -ENOMEM;
17871791
pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
17881792
attr = NULL;
17891793
}
@@ -1810,6 +1814,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
18101814
attr->nr = blk_num;
18111815
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
18121816
"counter%d", j);
1817+
if (!attr->dev_attr.attr.name)
1818+
return -ENOMEM;
18131819
pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
18141820
attr = NULL;
18151821

@@ -1821,6 +1827,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
18211827
attr->nr = blk_num;
18221828
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
18231829
"event%d", j);
1830+
if (!attr->dev_attr.attr.name)
1831+
return -ENOMEM;
18241832
pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
18251833
attr = NULL;
18261834
}
@@ -1853,6 +1861,8 @@ static int mlxbf_pmc_init_perftype_reg(struct device *dev, int blk_num)
18531861
attr->nr = blk_num;
18541862
attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
18551863
events[j].evt_name);
1864+
if (!attr->dev_attr.attr.name)
1865+
return -ENOMEM;
18561866
pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
18571867
attr = NULL;
18581868
i++;
@@ -1882,6 +1892,8 @@ static int mlxbf_pmc_create_groups(struct device *dev, int blk_num)
18821892
pmc->block[blk_num].block_attr_grp.attrs = pmc->block[blk_num].block_attr;
18831893
pmc->block[blk_num].block_attr_grp.name = devm_kasprintf(
18841894
dev, GFP_KERNEL, pmc->block_name[blk_num]);
1895+
if (!pmc->block[blk_num].block_attr_grp.name)
1896+
return -ENOMEM;
18851897
pmc->groups[pmc->group_num] = &pmc->block[blk_num].block_attr_grp;
18861898
pmc->group_num++;
18871899

0 commit comments

Comments
 (0)