diff mbox

mtd: fix null pointer deference when kzalloc returns null

Message ID 1437129472-13099-1-git-send-email-colin.king@canonical.com
State Accepted
Commit 7e0c19c9608483808e6b5c294e357fa456f058e1
Headers show

Commit Message

Colin Ian King July 17, 2015, 10:37 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

static analysis by smatch caught the following error:

drivers/mtd/maps/physmap_of.c:135 of_get_probes()
   error: potential null dereference 'res'.  (kzalloc returns null)

Check for failed kzalloc and return -ENOMEM in of_flash_probe if
this occurs.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/mtd/maps/physmap_of.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Brian Norris Aug. 19, 2015, 12:58 a.m. UTC | #1
On Fri, Jul 17, 2015 at 11:37:52AM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> static analysis by smatch caught the following error:
> 
> drivers/mtd/maps/physmap_of.c:135 of_get_probes()
>    error: potential null dereference 'res'.  (kzalloc returns null)
> 
> Check for failed kzalloc and return -ENOMEM in of_flash_probe if
> this occurs.
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Pushed to l2-mtd.git
diff mbox

Patch

diff --git a/drivers/mtd/maps/physmap_of.c b/drivers/mtd/maps/physmap_of.c
index 774b32f..3e614e9 100644
--- a/drivers/mtd/maps/physmap_of.c
+++ b/drivers/mtd/maps/physmap_of.c
@@ -130,6 +130,8 @@  static const char * const *of_get_probes(struct device_node *dp)
 			count++;
 
 	res = kzalloc((count + 1)*sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return NULL;
 	count = 0;
 	while (cplen > 0) {
 		res[count] = cp;
@@ -311,6 +313,10 @@  static int of_flash_probe(struct platform_device *dev)
 
 	ppdata.of_node = dp;
 	part_probe_types = of_get_probes(dp);
+	if (!part_probe_types) {
+		err = -ENOMEM;
+		goto err_out;
+	}
 	mtd_device_parse_register(info->cmtd, part_probe_types, &ppdata,
 			NULL, 0);
 	of_free_probes(part_probe_types);