regulator: axp20x: add NULL check on devm_kzalloc() return value
authorGustavo A. R. Silva <garsilva@embeddedor.com>
Thu, 6 Jul 2017 21:49:18 +0000 (16:49 -0500)
committerMark Brown <broonie@kernel.org>
Fri, 7 Jul 2017 12:48:33 +0000 (13:48 +0100)
Check return value from call to devm_kzalloc()
in order to prevent a NULL pointer dereference.

This issue was detected using Coccinelle and the following semantic patch:

@@
expression x;
identifier fld;
@@

* x = devm_kzalloc(...);
  ... when != x == NULL
  x->fld

Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/regulator/axp20x-regulator.c

index e2608fe770b9922c384d38ed86551d05acf09047..f18b36dd57dd44a2f1d94302f6e1492ea6e54f88 100644 (file)
@@ -691,6 +691,9 @@ static int axp20x_regulator_probe(struct platform_device *pdev)
                    (regulators == axp809_regulators && i == AXP809_DC1SW)) {
                        new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
                                                GFP_KERNEL);
+                       if (!new_desc)
+                               return -ENOMEM;
+
                        *new_desc = regulators[i];
                        new_desc->supply_name = dcdc1_name;
                        desc = new_desc;
@@ -700,6 +703,9 @@ static int axp20x_regulator_probe(struct platform_device *pdev)
                    (regulators == axp809_regulators && i == AXP809_DC5LDO)) {
                        new_desc = devm_kzalloc(&pdev->dev, sizeof(*desc),
                                                GFP_KERNEL);
+                       if (!new_desc)
+                               return -ENOMEM;
+
                        *new_desc = regulators[i];
                        new_desc->supply_name = dcdc5_name;
                        desc = new_desc;