Skip to contentSkip to conversation

staging: nvec: use strcmp() instead of strncmp() with magic length

PATCH Superseded Artem Lytkin 2 participants 2 messages Feb 5 – Feb 7

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

PATCH 1/1 staging: nvec: use strcmp() instead of strncmp() with magic length

Replace strncmp() with a hardcoded length of 30 with strcmp(). The bat_type string is already null-terminated (set two lines above), so strncmp() with an arbitrary length is misleading and functionally equivalent to strcmp().

---
 drivers/staging/nvec/nvec_power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/nvec/nvec_power.c +1 -1
index 2faab9fde..89dd997aa 100644
@@ -207,7 +207,7 @@ static int nvec_power_bat_notifier(struct notifier_block *nb,
 		 * This differs a little from the spec fill in more if you find
 		 * some.
 		 */
-		if (!strncmp(power->bat_type, "Li", 30))
+		if (!strcmp(power->bat_type, "Li"))
 			power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_LION;
 		else
 			power->bat_type_enum = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 15 lines · Artem Lytkin

Replace strncmp() with a hardcoded length of 30 with strcmp(). The bat_type string is already null-terminated (set two lines above), so strncmp() with an arbitrary length is misleading and functionally equivalent to strcmp().

---
 drivers/staging/nvec/nvec_power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/nvec/nvec_power.c +0 -0
index 2faab9fde..89dd997aa 100644

drivers/staging/nvec/nvec_power.c static int nvec_power_bat_notifier(struct notifier_block *nb, · line 210

Quoted 5 lines
drivers/staging/nvec/nvec_power.c +1 -1
@@ -207,7 +207,7 @@ static int nvec_power_bat_notifier(struct notifier_block *nb,
 		 * This differs a little from the spec fill in more if you find
 		 * some.
 		 */
-		if (!strncmp(power->bat_type, "Li", 30))
+		if (!strcmp(power->bat_type, "Li"))

Now the checking tools that look for "strcmp() is bad!" will come along and someone will send a patch that essencially reverts this :)

Please just leave this as-is, it's really fine and is not broken, right?

thanks,

greg k-h