Skip to contentSkip to conversation

leds: rgb: ktd202x: allocate the LED array for the chip's LED count

PATCH Superseded Cong Nguyen 2 participants 2 messages Nov 24 LED

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: rgb: ktd202x: allocate the LED array for the chip's LED count

ktd202x_probe() allocates chip->leds[] with one entry per LED child node. The allocation helper sets chip->num_leds to that count, but probe then overwrites it with the number of LEDs the chip supports (3 for KTD2026, 4 for KTD2027).

If the firmware describes fewer LEDs than the chip supports, ktd202x_chip_in_use() reads entries past the end of the allocation each time a brightness is set.

Allocate the array with the number of LEDs the chip supports, so that num_leds matches the allocation. Entries without a firmware node stay zeroed.

This issue was found by auditing the driver with an LLM coding assistant.

Fixes 0ebdb7210943 ("leds: Add ktd202x driver")
---
 drivers/leds/rgb/leds-ktd202x.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/leds/rgb/leds-ktd202x.c +4 -3
index f1822281c3e6..56f6fa906b70 100644
@@ -533,6 +533,7 @@ static int ktd202x_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
 	struct ktd202x *chip;
+	unsigned long num_leds;
 	int count;
 	int ret;
 
@@ -540,7 +541,9 @@ static int ktd202x_probe(struct i2c_client *client)
 	if (!count || count > KTD202X_MAX_LEDS)
 		return dev_err_probe(dev, -EINVAL, "Incorrect number of leds (%d)", count);
 
-	chip = devm_kzalloc_flex(dev, *chip, leds, count);
+	num_leds = (unsigned long)i2c_get_match_data(client);
+
+	chip = devm_kzalloc_flex(dev, *chip, leds, num_leds);
 	if (!chip)
 		return -ENOMEM;
 
@@ -558,8 +561,6 @@ static int ktd202x_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	chip->num_leds = (unsigned long)i2c_get_match_data(client);
-
 	chip->regulators[0].supply = "vin";
 	chip->regulators[1].supply = "vio";
 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(chip->regulators), chip->regulators);
Signature · 1 line
2.25.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] leds: ktd202x: uninitialized chip->num_leds in ktd202x_probe() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] leds: ktd202x: uninitialized chip->num_leds in ktd202x_probe() --

commit acafdda47f9cb14e885911a263b15aa32abaff09
Author: Cong Nguyen <[email protected]>

leds: rgb: ktd202x: allocate the LED array for the chip's LED count

This commit allocates chip->leds[] for the number of LEDs supported by the chip rather than the number of firmware child nodes, and removes the later assignment to chip->num_leds.

Quoted 24 lines
drivers/leds/rgb/leds-ktd202x.c +4 -1
index f1822281c3e61..56f6fa906b709 100644
@@ -533,6 +533,7 @@ static int ktd202x_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
 	struct ktd202x *chip;
+	unsigned long num_leds;
 	int count;
 	int ret;
 
@@ -540,7 +541,9 @@ static int ktd202x_probe(struct i2c_client *client)
 	if (!count || count > KTD202X_MAX_LEDS)
 		return dev_err_probe(dev, -EINVAL, "Incorrect number of leds (%d)", count);
 
-	chip = devm_kzalloc_flex(dev, *chip, leds, count);
+	num_leds = (unsigned long)i2c_get_match_data(client);
+
+	chip = devm_kzalloc_flex(dev, *chip, leds, num_leds);
 	if (!chip)
 		return -ENOMEM;
 

drivers/leds/rgb/leds-ktd202x.c static int ktd202x_probe(struct i2c_client *client) · line 561

Quoted 4 lines
drivers/leds/rgb/leds-ktd202x.c +0 -1
@@ -558,8 +561,6 @@ static int ktd202x_probe(struct i2c_client *client)
 	if (ret)
 		return ret;
 
-	chip->num_leds = (unsigned long)i2c_get_match_data(client);

[Severity: High]
Does removing this assignment leave chip->num_leds set to 0 on compilers without working __builtin_counted_by_ref() support (GCC < 15.1 and Clang < 22.1)?

When __builtin_counted_by_ref() is unavailable or CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF is set, __set_flex_counter() inside devm_kzalloc_flex() is a no-op and does not populate chip->num_leds. ktd202x_probe() -> ktd202x_probe_fw() then fails unconditionally with -EINVAL:

drivers/leds/rgb/leds-ktd202x.c:ktd202x_probe_fw() {
	count = device_get_child_node_count(dev);
	if (!count || count > chip->num_leds)
		return -EINVAL;
    ...
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1