diff mbox series

drivers:of: Add null check for bus in of_match_bus

Message ID 20240613150134.66329-1-gychoi.dev@gmail.com
State Rejected
Headers show
Series drivers:of: Add null check for bus in of_match_bus | expand

Checks

Context Check Description
robh/checkpatch success
robh/patch-applied fail build log

Commit Message

Gyeonggeon Choi June 13, 2024, 3:01 p.m. UTC
Added a null check for the bus variable after calling of_match_bus.
This prevents potential null pointer dereference errors in subsequent
code, where bus->count_cells could cause a crash if bus is NULL.

Signed-off-by: Gyeonggeon Choi <gychoi.dev@gmail.com>
---
 drivers/of/address.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Rob Herring (Arm) June 13, 2024, 3:44 p.m. UTC | #1
On Thu, Jun 13, 2024 at 9:02 AM Gyeonggeon Choi <gychoi.dev@gmail.com> wrote:
>
> Added a null check for the bus variable after calling of_match_bus.
> This prevents potential null pointer dereference errors in subsequent
> code, where bus->count_cells could cause a crash if bus is NULL.

Go read of_match_bus(). It never returns NULL and if somehow it did,
we'll BUG() first.

>
> Signed-off-by: Gyeonggeon Choi <gychoi.dev@gmail.com>
> ---
>  drivers/of/address.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/drivers/of/address.c b/drivers/of/address.c
> index d669ce25b5f9..85f986d25870 100644
> --- a/drivers/of/address.c
> +++ b/drivers/of/address.c
> @@ -504,6 +504,8 @@ static u64 __of_translate_address(struct device_node *node,
>         if (parent == NULL)
>                 return OF_BAD_ADDR;
>         bus = of_match_bus(parent);
> +       if (bus == NULL)
> +               return OF_BAD_ADDR;
>
>         /* Count address cells & copy address locally */
>         bus->count_cells(dev, &na, &ns);
> --
> 2.39.3 (Apple Git-146)
>
diff mbox series

Patch

diff --git a/drivers/of/address.c b/drivers/of/address.c
index d669ce25b5f9..85f986d25870 100644
--- a/drivers/of/address.c
+++ b/drivers/of/address.c
@@ -504,6 +504,8 @@  static u64 __of_translate_address(struct device_node *node,
 	if (parent == NULL)
 		return OF_BAD_ADDR;
 	bus = of_match_bus(parent);
+	if (bus == NULL)
+		return OF_BAD_ADDR;
 
 	/* Count address cells & copy address locally */
 	bus->count_cells(dev, &na, &ns);