diff mbox series

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

Message ID 202212231042356202397@zte.com.cn
State New
Headers show
Series [linux-next] power: supply: test-power: use strscpy() to instead of strncpy() | expand

Commit Message

Yang Yang Dec. 23, 2022, 2:42 a.m. UTC
From: Xu Panda <xu.panda@zte.com.cn>

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

Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
Signed-off-by: Yang Yang <yang.yang29@zte.com>
---
 drivers/power/supply/test_power.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Sebastian Reichel Feb. 3, 2023, 12:46 p.m. UTC | #1
Hi,

On Fri, Dec 23, 2022 at 10:42:35AM +0800, yang.yang29@zte.com.cn wrote:
> From: Xu Panda <xu.panda@zte.com.cn>
> 
> The implementation of strscpy() is more robust and safer.
> That's now the recommended way to copy NUL-terminated strings.
> 
> Signed-off-by: Xu Panda <xu.panda@zte.com.cn>
> Signed-off-by: Yang Yang <yang.yang29@zte.com>
> ---

Thanks, queued.

-- Sebastian

>  drivers/power/supply/test_power.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/power/supply/test_power.c b/drivers/power/supply/test_power.c
> index 5f510ddc946d..0d0a77584c5d 100644
> --- a/drivers/power/supply/test_power.c
> +++ b/drivers/power/supply/test_power.c
> @@ -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)
> -- 
> 2.15.2
diff mbox series

Patch

diff --git a/drivers/power/supply/test_power.c b/drivers/power/supply/test_power.c
index 5f510ddc946d..0d0a77584c5d 100644
--- a/drivers/power/supply/test_power.c
+++ b/drivers/power/supply/test_power.c
@@ -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)