Skip to contentSkip to conversation

platform/chrome: cros_ec_lightbar: replace sscanf with kstrtouint

PATCH In review trunix-creator 2 participants 2 messages Feb 8 – Feb 23

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

PATCH 1/1 platform/chrome: cros_ec_lightbar: replace sscanf with kstrtouint

Replace the use of sscanf() with kstrtouint(), which is the favored kernel API for parsing integers from strings. This avoids format string parsing overhead and improves robustness.

---
 drivers/platform/chrome/cros_ec_lightbar.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/platform/chrome/cros_ec_lightbar.c +2 -2
index 922758c2cec1..e455ff440b11 100644
@@ -243,10 +243,10 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
 		if (!*buf)
 			break;
 
-		ret = sscanf(buf, "%i", &val[i++]);
+		ret = kstrtouint(buf, 0, &val[i]);
 		if (ret == 0)
 			goto exit;
-
+		i++;
 		if (i == 4) {
 			param = (struct ec_params_lightbar *)msg->data;
 			param->cmd = LIGHTBAR_CMD_SET_RGB;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 5 lines · trunix-creator

Replace the use of sscanf() with kstrtouint(), which is the favored kernel API for parsing integers from strings. This avoids format string parsing overhead and improves robustness.

Thanks for the patch.

Before this can be considered for merging, you'll need to update the Signed-off-by line. The kernel community requires the use of your real name (legal name) rather than a handle (see also "Developer's Certificate of Origin" in Documentation/process/submitting-patches.rst).

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, · line 248

Quoted 8 lines
drivers/platform/chrome/cros_ec_lightbar.c +1 -1
@@ -243,10 +243,10 @@ static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
 		if (!*buf)
 			break;
 
-		ret = sscanf(buf, "%i", &val[i++]);
+		ret = kstrtouint(buf, 0, &val[i]);
 		if (ret == 0)
 			goto exit;

This is incorrect. sscanf returns the number of items matched (e.g., 1 on success in the case). kstrtouint returns 0 on success.

drivers/platform/chrome/cros_ec_lightbar.c static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, · line 249

Quoted 2 lines
drivers/platform/chrome/cros_ec_lightbar.c +1 -1
-
+		i++;

Why it moves the increment to here? It seems unrelated to the patch.