Re: [PATCH] perf/x86: Replace strncpy() with memcpy() for vendor string

From: David Laight
Date: Fri Jul 04 2025 - 05:25:41 EST


On Thu, 19 Jun 2025 03:28:43 +0530
Usman Akinyemi <usmanakinyemi202@xxxxxxxxx> wrote:

> strncpy() is unsafe for fixed-size binary data as
> it may not NUL-terminate and is deprecated for such
> usage. Since we're copying raw CPUID register values,
> memcpy() is the correct and safe choice.
>
> Signed-off-by: Usman Akinyemi <usmanakinyemi202@xxxxxxxxx>
> ---
> tools/perf/arch/x86/util/header.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/arch/x86/util/header.c b/tools/perf/arch/x86/util/header.c
> index 412977f8aa83..43ba55627817 100644
> --- a/tools/perf/arch/x86/util/header.c
> +++ b/tools/perf/arch/x86/util/header.c
> @@ -16,9 +16,9 @@ void get_cpuid_0(char *vendor, unsigned int *lvl)
> unsigned int b, c, d;
>
> cpuid(0, 0, lvl, &b, &c, &d);
> - strncpy(&vendor[0], (char *)(&b), 4);
> - strncpy(&vendor[4], (char *)(&d), 4);
> - strncpy(&vendor[8], (char *)(&c), 4);
> + memcpy(&vendor[0], (char *)(&b), 4);
> + memcpy(&vendor[4], (char *)(&d), 4);
> + memcpy(&vendor[8], (char *)(&c), 4);

Why not:
cpuid(0, 0, lvl, (void *)vendor, (void *)(vendor + 8), (void *)(vendor + 4));


> vendor[12] = '\0';
> }
>