diff mbox series

[3/4] ACPI: battery: Replace strncpy() with strscpy()

Message ID 20230114085053.72059-4-W_Armin@gmx.de (mailing list archive)
State Changes Requested
Headers show
Series ACPI: battery: Fix various string handling issues | expand

Commit Message

Armin Wolf Jan. 14, 2023, 8:50 a.m. UTC
Currently, strncpy() and manual NUL-termination is used
when copying integers. Switch to strscpy() which takes care
of NUL-terminating the result.

Signed-off-by: Armin Wolf <W_Armin@gmx.de>
---
 drivers/acpi/battery.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

--
2.30.2

Comments

Rafael J. Wysocki Jan. 17, 2023, 2:44 p.m. UTC | #1
On Sat, Jan 14, 2023 at 9:51 AM Armin Wolf <W_Armin@gmx.de> wrote:
>
> Currently, strncpy() and manual NUL-termination is used
> when copying integers. Switch to strscpy() which takes care
> of NUL-terminating the result.
>
> Signed-off-by: Armin Wolf <W_Armin@gmx.de>
> ---
>  drivers/acpi/battery.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
> index 9f6daa9f2010..b39b84b8f3ae 100644
> --- a/drivers/acpi/battery.c
> +++ b/drivers/acpi/battery.c
> @@ -449,8 +449,7 @@ static int extract_package(struct acpi_battery *battery,
>
>                                 break;
>                         case ACPI_TYPE_INTEGER:
> -                               strncpy(ptr, (u8 *)&element->integer.value, sizeof(u64));
> -                               ptr[sizeof(u64)] = 0;
> +                               strscpy(ptr, (u8 *)&element->integer.value, sizeof(u64) + 1);
>
>                                 break;
>                         default:
> --

This can be folded into the previous patch.

Otherwise you're patching your own patch which isn't particularly clean.
diff mbox series

Patch

diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c
index 9f6daa9f2010..b39b84b8f3ae 100644
--- a/drivers/acpi/battery.c
+++ b/drivers/acpi/battery.c
@@ -449,8 +449,7 @@  static int extract_package(struct acpi_battery *battery,

 				break;
 			case ACPI_TYPE_INTEGER:
-				strncpy(ptr, (u8 *)&element->integer.value, sizeof(u64));
-				ptr[sizeof(u64)] = 0;
+				strscpy(ptr, (u8 *)&element->integer.value, sizeof(u64) + 1);

 				break;
 			default: