Skip to contentSkip to conversation

mtd: nand: vf610_nfc: add NULL check on of_match_device() return value

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Jul 7, 2017 – Jul 17, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: nand: vf610_nfc: add NULL check on of_match_device() return value

Check return value from call to of_match_device()
in order to prevent a NULL pointer dereference.

In case of NULL print error message and return -ENODEV

---
 drivers/mtd/nand/vf610_nfc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/vf610_nfc.c +5 -0
index 744ab10..ca0ab96 100644
@@ -674,6 +674,11 @@ static int vf610_nfc_probe(struct platform_device *pdev)
 	}
 
 	of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
+	if (!of_id) {
+		dev_err(&pdev->dev, "Failed to match device!\n");
+		return -ENODEV;
+	}
+
 	nfc->variant = (enum vf610_nfc_variant)of_id->data;
 
 	for_each_available_child_of_node(nfc->dev->of_node, child) {
Signature · 1 line
2.5.0
#1 lore raw
Quoted 15 lines · Gustavo A. R. Silva

Check return value from call to of_match_device()
in order to prevent a NULL pointer dereference.

In case of NULL print error message and return -ENODEV

---
 drivers/mtd/nand/vf610_nfc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/vf610_nfc.c +0 -0
index 744ab10..ca0ab96 100644

drivers/mtd/nand/vf610_nfc.c static int vf610_nfc_probe(struct platform_device *pdev) · line 681

Quoted 8 lines
drivers/mtd/nand/vf610_nfc.c +5 -0
@@ -674,6 +674,11 @@ static int vf610_nfc_probe(struct platform_device *pdev)
 	}
 
 	of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
+	if (!of_id) {
+		dev_err(&pdev->dev, "Failed to match device!\n");
+		return -ENODEV;
+	}
+

While this check is functionally correct, this case cannot happen, because this is DT-only driver, and without a valid match in vf610_nfc_dt_ids the dev wouldn't have been probed in the first place.

I'll let Stefan decide whether he wants it or not, but I see no real reason for this extra check.

Quoted 3 lines
drivers/mtd/nand/vf610_nfc.c +0 -0
 	nfc->variant = (enum vf610_nfc_variant)of_id->data;
 
 	for_each_available_child_of_node(nfc->dev->of_node, child) {
#2 lore raw
Quoted 31 lines · Boris Brezillon
Quoted 15 lines · Gustavo A. R. Silva

Check return value from call to of_match_device()
in order to prevent a NULL pointer dereference.

In case of NULL print error message and return -ENODEV

---
 drivers/mtd/nand/vf610_nfc.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mtd/nand/vf610_nfc.c +0 -0
index 744ab10..ca0ab96 100644

drivers/mtd/nand/vf610_nfc.c static int vf610_nfc_probe(struct platform_device *pdev) · line 681

Quoted 8 lines
drivers/mtd/nand/vf610_nfc.c +5 -0
@@ -674,6 +674,11 @@ static int vf610_nfc_probe(struct platform_device *pdev)
 	}
 
 	of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
+	if (!of_id) {
+		dev_err(&pdev->dev, "Failed to match device!\n");
+		return -ENODEV;
+	}
+

While this check is functionally correct, this case cannot happen, because this is DT-only driver, and without a valid match in vf610_nfc_dt_ids the dev wouldn't have been probed in the first place.

Quoted 2 lines

I'll let Stefan decide whether he wants it or not, but I see no real reason for this extra check.

So how did you trigger the issue in the first place ?

Signature · 2 lines
Best regards,
Marek Vasut