From: Thorsten Blum Date: Thu, 13 Mar 2025 13:30:02 +0000 (+0100) Subject: watchdog/perf: optimize bytes copied and remove manual NUL-termination X-Git-Tag: v6.15-rc1~80^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6164be01f1799458b5c6f59a547d6241e4a4b4d6;p=thirdparty%2Fkernel%2Flinux.git watchdog/perf: optimize bytes copied and remove manual NUL-termination Currently, up to 23 bytes of the source string are copied to the destination buffer (including the comma and anything after it), only to then manually NUL-terminate the destination buffer again at index 'len' (where the comma was found). Fix this by calling strscpy() with 'len' instead of the destination buffer size to copy only as many bytes from the source string as needed. Change the length check to allow 'len' to be less than or equal to the destination buffer size to fill the whole buffer if needed. Remove the if-check for the return value of strscpy(), because calling strscpy() with 'len' always truncates the source string at the comma as expected and NUL-terminates the destination buffer at the corresponding index instead. Remove the manual NUL-termination. No functional changes intended. Link: https://lkml.kernel.org/r/20250313133004.36406-2-thorsten.blum@linux.dev Signed-off-by: Thorsten Blum Cc: Song Liu Cc: Thomas Gleinxer Signed-off-by: Andrew Morton --- diff --git a/kernel/watchdog_perf.c b/kernel/watchdog_perf.c index 59c1d86a73a24..b81167cb0dfc9 100644 --- a/kernel/watchdog_perf.c +++ b/kernel/watchdog_perf.c @@ -294,12 +294,10 @@ void __init hardlockup_config_perf_event(const char *str) } else { unsigned int len = comma - str; - if (len >= sizeof(buf)) + if (len > sizeof(buf)) return; - if (strscpy(buf, str, sizeof(buf)) < 0) - return; - buf[len] = 0; + strscpy(buf, str, len); if (kstrtoull(buf, 16, &config)) return; }