platform/mellanox: Add null pointer checks for devm_kasprintf()
authorKunwu Chan <chentao@kylinos.cn>
Fri, 1 Dec 2023 05:54:47 +0000 (13:54 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 13 Dec 2023 17:45:05 +0000 (18:45 +0100)
[ Upstream commit 2c7c857f5fed997be93047d2de853d7f10c8defe ]

devm_kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure.

Compile-tested only.

Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
Suggested-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Suggested-by: Vadim Pasternak <vadimp@nvidia.com>
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: Vadim Pasternak <vadimp@nvidia.com>
Link: https://lore.kernel.org/r/20231201055447.2356001-1-chentao@kylinos.cn
[ij: split the change into two]
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/platform/mellanox/mlxbf-pmc.c

index 2d4bbe9..925bfc4 100644 (file)
@@ -1202,6 +1202,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
        attr->dev_attr.show = mlxbf_pmc_event_list_show;
        attr->nr = blk_num;
        attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL, "event_list");
+       if (!attr->dev_attr.attr.name)
+               return -ENOMEM;
        pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
        attr = NULL;
 
@@ -1214,6 +1216,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
                attr->nr = blk_num;
                attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
                                                          "enable");
+               if (!attr->dev_attr.attr.name)
+                       return -ENOMEM;
                pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
                attr = NULL;
        }
@@ -1240,6 +1244,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
                attr->nr = blk_num;
                attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
                                                          "counter%d", j);
+               if (!attr->dev_attr.attr.name)
+                       return -ENOMEM;
                pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
                attr = NULL;
 
@@ -1251,6 +1257,8 @@ static int mlxbf_pmc_init_perftype_counter(struct device *dev, int blk_num)
                attr->nr = blk_num;
                attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
                                                          "event%d", j);
+               if (!attr->dev_attr.attr.name)
+                       return -ENOMEM;
                pmc->block[blk_num].block_attr[++i] = &attr->dev_attr.attr;
                attr = NULL;
        }
@@ -1283,6 +1291,8 @@ static int mlxbf_pmc_init_perftype_reg(struct device *dev, int blk_num)
                attr->nr = blk_num;
                attr->dev_attr.attr.name = devm_kasprintf(dev, GFP_KERNEL,
                                                          events[j].evt_name);
+               if (!attr->dev_attr.attr.name)
+                       return -ENOMEM;
                pmc->block[blk_num].block_attr[i] = &attr->dev_attr.attr;
                attr = NULL;
                i++;
@@ -1311,6 +1321,8 @@ static int mlxbf_pmc_create_groups(struct device *dev, int blk_num)
        pmc->block[blk_num].block_attr_grp.attrs = pmc->block[blk_num].block_attr;
        pmc->block[blk_num].block_attr_grp.name = devm_kasprintf(
                dev, GFP_KERNEL, pmc->block_name[blk_num]);
+       if (!pmc->block[blk_num].block_attr_grp.name)
+               return -ENOMEM;
        pmc->groups[blk_num] = &pmc->block[blk_num].block_attr_grp;
 
        return 0;