Re: [PATCH v4 2/5] perf tools riscv: Add support for get_cpuid_str function

From: Nikita Shubin
Date: Sat Jun 25 2022 - 01:28:27 EST


Hello Arnaldo!

On Fri, 24 Jun 2022 13:32:20 -0300
Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:

> > + if (mimpid)
> > + free(mimpid);
>
> just use:
>
>
> free(mvendorid);
> free(marchid);
> free(mimpid);
>
> fewer lines, free() accepts NULL.
>
> > +
> > + return cpuid;
> > +}
> > +
> > +int get_cpuid(char *buffer, size_t sz)
> > +{
> > + char *cpuid = _get_cpuid();
> > +
> > + if (sz < strlen(cpuid)) {
> > + free(cpuid);
> > + return -EINVAL;
> > + }
> > +
> > + scnprintf(buffer, sz, "%s", cpuid);
>
> You're leaking cpuid here.

Agree with both.

Thank you for pointing it out.

>
> > + return 0;
> > +}
> > +
> > +char *
> > +get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
> > +{
> > + return _get_cpuid();
> > +}
> > --
> > 2.35.1
>