Skip to content

Commit 9cca73d

Browse files
JustinStittkees
authored andcommitted
hwmon: (acpi_power_meter) replace open-coded kmemdup_nul
`strncpy` is deprecated for use on NUL-terminated destination strings [1]. Let's refactor this kcalloc() + strncpy() into a kmemdup_nul() which has more obvious behavior and is less error prone. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: KSPP#90 Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt <justinstitt@google.com> Reviewed-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20230926-strncpy-drivers-hwmon-acpi_power_meter-c-v5-1-3fc31a9daf99@google.com Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent fed2ef7 commit 9cca73d

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

drivers/hwmon/acpi_power_meter.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,14 +796,13 @@ static int read_capabilities(struct acpi_power_meter_resource *resource)
796796
goto error;
797797
}
798798

799-
*str = kcalloc(element->string.length + 1, sizeof(u8),
800-
GFP_KERNEL);
799+
*str = kmemdup_nul(element->string.pointer, element->string.length,
800+
GFP_KERNEL);
801801
if (!*str) {
802802
res = -ENOMEM;
803803
goto error;
804804
}
805805

806-
strncpy(*str, element->string.pointer, element->string.length);
807806
str++;
808807
}
809808

0 commit comments

Comments
 (0)