Skip to contentSkip to conversation

clk: bcm: Add NULL check in raspberrypi_clk_register()

PATCH Superseded Henry Martin 4 participants 6 messages Apr 1, 2025 – May 6, 2025

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

PATCH 1/1 clk: bcm: Add NULL check in raspberrypi_clk_register()

devm_kasprintf() returns NULL when memory allocation fails. Currently, raspberrypi_clk_register() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes 7dad8a613185 ("clk: bcm: rpi: Give firmware clocks a name")
---
 drivers/clk/bcm/clk-raspberrypi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +2 -0
index 0e1fe3759530..720acc10f8aa 100644
@@ -286,6 +286,8 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
 	init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
 				   "fw-clk-%s",
 				   rpi_firmware_clk_names[id]);
+	if (!init.name)
+		return ERR_PTR(-ENOMEM);
 	init.ops = &raspberrypi_firmware_clk_ops;
 	init.flags = CLK_GET_RATE_NOCACHE;
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Henry

Thanks for the patch.

Quoted 8 lines · Henry Martin

devm_kasprintf() returns NULL when memory allocation fails. Currently, raspberrypi_clk_register() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes 7dad8a613185 ("clk: bcm: rpi: Give firmware clocks a name")

Nit: It was 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks") that introduced the devm_kasprintf. 7dad8a613185 only changed the thing being printed.

Quoted 1 line
Quoted 20 lines
---
 drivers/clk/bcm/clk-raspberrypi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +2 -1
index 0e1fe3759530..720acc10f8aa 100644
@@ -286,6 +286,8 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
        init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
                                   "fw-clk-%s",
                                   rpi_firmware_clk_names[id]);
+       if (!init.name)
+               return ERR_PTR(-ENOMEM);
        init.ops = &raspberrypi_firmware_clk_ops;
        init.flags = CLK_GET_RATE_NOCACHE;
 
--

2.34.1

#2 lore raw

PATCH v2 1/1 clk: bcm: rpi: Add NULL check in raspberrypi_clk_register()

devm_kasprintf() returns NULL when memory allocation fails. Currently, raspberrypi_clk_register() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")

---
V1 -> V2: Correct the commit hash in the Fixes: tag.

 drivers/clk/bcm/clk-raspberrypi.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +2 -0
index 0e1fe3759530..720acc10f8aa 100644
@@ -286,6 +286,8 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
 	init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
 				   "fw-clk-%s",
 				   rpi_firmware_clk_names[id]);
+	if (!init.name)
+		return ERR_PTR(-ENOMEM);
 	init.ops = &raspberrypi_firmware_clk_ops;
 	init.flags = CLK_GET_RATE_NOCACHE;
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#3 lore raw

Re: [PATCH v2] clk: bcm: rpi: Add NULL check in raspberrypi_clk_register()

Am 02.04.25 um 04:05 schrieb Henry Martin:

Quoted 9 lines

devm_kasprintf() returns NULL when memory allocation fails. Currently, raspberrypi_clk_register() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")

Just a note, please don't send new patch versions as a reply to older ones. This makes them harder to find.

Thanks

Quoted 19 lines

---
V1 -> V2: Correct the commit hash in the Fixes: tag.

  drivers/clk/bcm/clk-raspberrypi.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +2 -0
index 0e1fe3759530..720acc10f8aa 100644
@@ -286,6 +286,8 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
  	init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
  				   "fw-clk-%s",
  				   rpi_firmware_clk_names[id]);
+	if (!init.name)
+		return ERR_PTR(-ENOMEM);
  	init.ops = &raspberrypi_firmware_clk_ops;
  	init.flags = CLK_GET_RATE_NOCACHE;
 
#4 lore raw

Re: [PATCH v2] clk: bcm: rpi: Add NULL check in raspberrypi_clk_register()

Quoted 2 lines

Just a note, please don't send new patch versions as a reply to older ones. This makes them harder to find.

Noted, thanks for the review! I'll make sure to send new patch versions as separate emails in the future.

Regards,
Henry

Quoted 37 lines · Stefan Wahren <[email protected]> 于2025年4月17日周四 18:09写道:

Am 02.04.25 um 04:05 schrieb Henry Martin:

Quoted 9 lines

devm_kasprintf() returns NULL when memory allocation fails. Currently, raspberrypi_clk_register() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")

Just a note, please don't send new patch versions as a reply to older ones. This makes them harder to find.

Thanks

Quoted 19 lines

---
V1 -> V2: Correct the commit hash in the Fixes: tag.

  drivers/clk/bcm/clk-raspberrypi.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +2 -0
index 0e1fe3759530..720acc10f8aa 100644
@@ -286,6 +286,8 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
      init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
                                 "fw-clk-%s",
                                 rpi_firmware_clk_names[id]);
+     if (!init.name)
+             return ERR_PTR(-ENOMEM);
      init.ops = &raspberrypi_firmware_clk_ops;
      init.flags = CLK_GET_RATE_NOCACHE;
 
maintainer Applied
#5 lore raw

Re: [PATCH v2] clk: bcm: rpi: Add NULL check in raspberrypi_clk_register()

In reply to Henry Martin

Quoting Henry Martin (2025-04-01 19:05:13)

Quoted 10 lines

devm_kasprintf() returns NULL when memory allocation fails. Currently, raspberrypi_clk_register() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes 93d2725affd6 ("clk: bcm: rpi: Discover the firmware clocks")

---

Applied to clk-next