Skip to contentSkip to conversation

backlight: pm8941: Add NULL check in wled_configure()

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

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

PATCH v3 1/1 backlight: pm8941: Add NULL check in wled_configure()

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

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

Fixes f86b77583d88 ("backlight: pm8941: Convert to using %pOFn instead of device_node.name")

---
V2 -> V3: Correct commit meessage and confirm this patch has considered resource cleanup to avoid any subsequent issues, ensuring that errors are handled properly and no resources are left in an inconsistent state.
V1 -> V2: Fix commit message to use imperative mood and wrap lines to 75 characters.

 drivers/video/backlight/qcom-wled.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/video/backlight/qcom-wled.c +4 -2
index 9afe701b2a1b..a63bb42c8f8b 100644
@@ -1406,9 +1406,11 @@ static int wled_configure(struct wled *wled)
 	wled->ctrl_addr = be32_to_cpu(*prop_addr);
 
 	rc = of_property_read_string(dev->of_node, "label", &wled->name);
-	if (rc)
+	if (rc) {
 		wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
-
+		if (!wled->name)
+			return -ENOMEM;
+	}
 	switch (wled->version) {
 	case 3:
 		u32_opts = wled3_opts;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line

devm_kasprintf() return NULL if memory allocation fails. Currently,

…
call? failed?

Quoted 1 line

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

Do you propose to improve this function implementation a bit more?

Regards,
Markus

#2 lore raw

PATCH v3 1/1 backlight: pm8941: Add NULL check in wled_configure()

Thanks for your review. No further improvements needed for this implementation
- it already handles all error cases appropriately.

Best regards,
Henry

#3 lore raw

PATCH v3 1/1 backlight: pm8941: Add NULL check in wled_configure()

In reply to Markus Elfring

The function wled_configure() uses devm_kasprintf() without checking for allocation failures, which could lead to NULL pointer dereferences.

Add proper error handling when devm_kasprintf() fails by:
- Returning -ENOMEM immediately
- Ensuring no resources are left allocated (none need cleanup in this case)

Fixes f86b77583d88 ("backlight: pm8941: Convert to using %pOFn instead of device_node.name")

---
V2 -> V3: Fix commit message and verify proper error handling with resource cleanup.
V1 -> V2: Fix commit message to use imperative mood and wrap lines to 75 characters.

 drivers/video/backlight/qcom-wled.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/video/backlight/qcom-wled.c +4 -2
index 9afe701b2a1b..a63bb42c8f8b 100644
@@ -1406,9 +1406,11 @@ static int wled_configure(struct wled *wled)
 	wled->ctrl_addr = be32_to_cpu(*prop_addr);
 
 	rc = of_property_read_string(dev->of_node, "label", &wled->name);
-	if (rc)
+	if (rc) {
 		wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
-
+		if (!wled->name)
+			return -ENOMEM;
+	}
 	switch (wled->version) {
 	case 3:
 		u32_opts = wled3_opts;
Signature · 1 line
2.34.1
maintainer
#4 lore raw
Quoted 6 lines · Henry Martin

The function wled_configure() uses devm_kasprintf() without checking for allocation failures, which could lead to NULL pointer dereferences.

Add proper error handling when devm_kasprintf() fails by:
- Returning -ENOMEM immediately
- Ensuring no resources are left allocated (none need cleanup in this case)

Two things have gone wrong here:

1. This patch has been incorrectly posted with the wrong patch number.
2. You've let Markus bully you into adding some pointless text in the patch description ;-). I think the original v3 was better worded. IMHO the bulleted list adds nothing useful.

When you get the chance please send a v4 of the patch so we can pick it up without any confusion (I suspect it might be a week or two before this gets pulled so clean mail threads help a lot). It's up to you but I'd recommend keeping the original v3 wording and label it something like:
V3 -> V4: No functional changes, just correcting the version number

Daniel.

Quoted 34 lines
Fixes f86b77583d88 ("backlight: pm8941: Convert to using %pOFn instead of device_node.name")

---
V2 -> V3: Fix commit message and verify proper error handling with resource cleanup.
V1 -> V2: Fix commit message to use imperative mood and wrap lines to 75 characters.

 drivers/video/backlight/qcom-wled.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/video/backlight/qcom-wled.c +4 -3
index 9afe701b2a1b..a63bb42c8f8b 100644
@@ -1406,9 +1406,11 @@ static int wled_configure(struct wled *wled)
 	wled->ctrl_addr = be32_to_cpu(*prop_addr);
 
 	rc = of_property_read_string(dev->of_node, "label", &wled->name);
-	if (rc)
+	if (rc) {
 		wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
-
+		if (!wled->name)
+			return -ENOMEM;
+	}
 	switch (wled->version) {
 	case 3:
 		u32_opts = wled3_opts;
--

2.34.1

#5 lore raw

PATCH v4 1/1 backlight: pm8941: Add NULL check in wled_configure()

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

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

Fixes f86b77583d88 ("backlight: pm8941: Convert to using %pOFn instead of device_node.name")

---
V3 -> V4: No functional changes, just correcting the version number V2 -> V3: Correct commit meessage and confirm this patch has considered resource cleanup to avoid any subsequent issues, ensuring that errors are handled properly and no resources are left in an inconsistent state.
V1 -> V2: Fix commit message to use imperative mood and wrap lines to 75 characters.

 drivers/video/backlight/qcom-wled.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/video/backlight/qcom-wled.c +4 -2
index 9afe701b2a1b..a63bb42c8f8b 100644
@@ -1406,9 +1406,11 @@ static int wled_configure(struct wled *wled)
 	wled->ctrl_addr = be32_to_cpu(*prop_addr);
 
 	rc = of_property_read_string(dev->of_node, "label", &wled->name);
-	if (rc)
+	if (rc) {
 		wled->name = devm_kasprintf(dev, GFP_KERNEL, "%pOFn", dev->of_node);
-
+		if (!wled->name)
+			return -ENOMEM;
+	}
 	switch (wled->version) {
 	case 3:
 		u32_opts = wled3_opts;
Signature · 1 line
2.34.1
maintainer Applied
#6 lore raw

Re: (subset) [PATCH v4] backlight: pm8941: Add NULL check in wled_configure()

Quoted 8 lines · Henry Martin

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

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

[...]

Applied, thanks!

[1/1] backlight: pm8941: Add NULL check in wled_configure()

      commit: b0fdeb96ead46de57a6226bc3a3ac7f9b50c0ace

--
Lee Jones [李琼斯]

maintainer
#7 lore raw

Re: (subset) [PATCH v4] backlight: pm8941: Add NULL check in wled_configure()

Quoted 8 lines · Lee Jones
Quoted 7 lines · Henry Martin

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

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

Quoted 6 lines
Quoted 1 line

[...]

Applied, thanks!

[1/1] backlight: pm8941: Add NULL check in wled_configure()

      commit: b0fdeb96ead46de57a6226bc3a3ac7f9b50c0ace

Next time, please send subsequent patch versions independently.

Signature · 1 line
Lee Jones [李琼斯]
#8 lore raw

Re: (subset) [PATCH v4] backlight: pm8941: Add NULL check in wled_configure()

Noted, thanks for the feedback! I’ll send subsequent versions as independent patches in the future.

Best regards,
Henry

Quoted 22 lines · Lee Jones <[email protected]> 于2025年4月4日周五 23:13写道:
Quoted 8 lines · Lee Jones
Quoted 7 lines · Henry Martin

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

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

Quoted 6 lines
Quoted 1 line

[...]

Applied, thanks!

[1/1] backlight: pm8941: Add NULL check in wled_configure()

      commit: b0fdeb96ead46de57a6226bc3a3ac7f9b50c0ace

Next time, please send subsequent patch versions independently.

--
Lee Jones [李琼斯]

#9 lore raw
In reply to Daniel Thompson

…

Quoted 1 line

patch description ;-). I think the original v3 was better worded.

…

Can you find the mentioning of adjustments helpful for better error handling?

Regards,
Markus

maintainer Reviewed-by on v3 1/1
#10 lore raw
In reply to Henry Martin
Quoted 9 lines · Henry Martin

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

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

Fixes f86b77583d88 ("backlight: pm8941: Convert to using %pOFn instead of device_node.name")

Daniel.