Skip to contentSkip to conversation

fbdev: atmel_lcdfb: Fix potential NULL dereference

PATCH In review Salah Triki 2 participants 2 messages Jul 24, 2025

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

PATCH 1/1 fbdev: atmel_lcdfb: Fix potential NULL dereference

of_match_device() returns NULL in case of failure, so check its return value before casting and accessing to data field in order to prevent NULL dereference.

---
 drivers/video/fbdev/atmel_lcdfb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/video/fbdev/atmel_lcdfb.c +6 -2
index 9dfbc5310210..b1017ac9c73b 100644
@@ -922,6 +922,7 @@ static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
 	struct device *dev = &sinfo->pdev->dev;
 	struct device_node *np =dev->of_node;
 	struct device_node *display_np;
+	const struct of_device_id *match;
 	struct atmel_lcdfb_power_ctrl_gpio *og;
 	bool is_gpio_power = false;
 	struct fb_videomode fb_vm;
@@ -930,8 +931,11 @@ static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
 	int ret;
 	int i;
 
-	sinfo->config = (struct atmel_lcdfb_config*)
-		of_match_device(atmel_lcdfb_dt_ids, dev)->data;
+	match = of_match_device(atmel_lcdfb_dt_ids, dev);
+	if (!match)
+		return -ENODEV;
+
+	sinfo->config = (struct atmel_lcdfb_config *)match->data;
 
 	display_np = of_parse_phandle(np, "display", 0);
 	if (!display_np) {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 22 lines · Salah Triki

of_match_device() returns NULL in case of failure, so check its return value before casting and accessing to data field in order to prevent NULL dereference.

---
 drivers/video/fbdev/atmel_lcdfb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/video/fbdev/atmel_lcdfb.c +1 -0
index 9dfbc5310210..b1017ac9c73b 100644
@@ -922,6 +922,7 @@ static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
 	struct device *dev = &sinfo->pdev->dev;
 	struct device_node *np =dev->of_node;
 	struct device_node *display_np;
+	const struct of_device_id *match;
 	struct atmel_lcdfb_power_ctrl_gpio *og;
 	bool is_gpio_power = false;
 	struct fb_videomode fb_vm;

drivers/video/fbdev/atmel_lcdfb.c static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo) · line 936

Quoted 8 lines
drivers/video/fbdev/atmel_lcdfb.c +3 -2
@@ -930,8 +931,11 @@ static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
 	int ret;
 	int i;
 
-	sinfo->config = (struct atmel_lcdfb_config*)
-		of_match_device(atmel_lcdfb_dt_ids, dev)->data;
+	match = of_match_device(atmel_lcdfb_dt_ids, dev);
+	if (!match)
+		return -ENODEV;

This is dead code, it will never happen because atmel_lcdfb_of_init is only called from atmel_lcdfb_probe which will only be called when there is a match.

Quoted 8 lines
drivers/video/fbdev/atmel_lcdfb.c +2 -0
+
+	sinfo->config = (struct atmel_lcdfb_config *)match->data;
 
 	display_np = of_parse_phandle(np, "display", 0);
 	if (!display_np) {
Signature · 1 line
2.43.0
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com