Skip to contentSkip to conversation

power: supply: test-power: use strscpy() to instead of strncpy()

PATCH In review [email protected] 2 participants 2 messages Dec 23, 2022 – Feb 3, 2023 Power supply

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: supply: test-power: use strscpy() to instead of strncpy()

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

---
 drivers/power/supply/test_power.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/test_power.c +1 -2
index 5f510ddc946d..0d0a77584c5d 100644
@@ -306,8 +306,7 @@ static int map_get_value(struct battery_property_map *map, const char *key,
 	char buf[MAX_KEYLENGTH];
 	int cr;
 
-	strncpy(buf, key, MAX_KEYLENGTH);
-	buf[MAX_KEYLENGTH-1] = '\0';
+	strscpy(buf, key, MAX_KEYLENGTH);
 
 	cr = strnlen(buf, MAX_KEYLENGTH) - 1;
 	if (cr < 0)
Signature · 1 line
2.15.2
#1 lore raw

Re: [PATCH linux-next] power: supply: test-power: use strscpy() to instead of? strncpy()

Hi,

Quoted 8 lines · On Fri, Dec 23, 2022 at 10:42:35AM +0800, [email protected] wrote:

From: Xu Panda <[email protected]>

The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings.

---

Thanks, queued.

-- Sebastian

Quoted 19 lines
 drivers/power/supply/test_power.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/power/supply/test_power.c +1 -2
index 5f510ddc946d..0d0a77584c5d 100644
@@ -306,8 +306,7 @@ static int map_get_value(struct battery_property_map *map, const char *key,
 	char buf[MAX_KEYLENGTH];
 	int cr;
 
-	strncpy(buf, key, MAX_KEYLENGTH);
-	buf[MAX_KEYLENGTH-1] = '\0';
+	strscpy(buf, key, MAX_KEYLENGTH);
 
 	cr = strnlen(buf, MAX_KEYLENGTH) - 1;
 	if (cr < 0)
Signature · 1 line
2.15.2