Re: [PATCH 09/12] perf jitdump: Use dirname() return value in jit_open()

Ian Rogers <[email protected]> Wed, 5 Aug 2026 12:16:40 -0700
Newsgroups org.kernel.vger.linux-perf-users,org.kernel.vger.linux-kernel
Message-ID <CAP-5=fViJUQge4V1mVUaO37qarnsCTwpKzX2kqRZT14EvuN-Fg@mail.gmail.com>
On Wed, Aug 5, 2026 at 6:32 AM Arnaldo Carvalho de Melo <[email protected]> wrote:
>
> From: Arnaldo Carvalho de Melo <[email protected]>
>
> jit_open() calls dirname(jd->dir) but ignores the return value.  POSIX
> says dirname() may return a pointer to internal static storage — glibc
> does this when the path has no '/', returning "." from a static buffer
> and leaving jd->dir unchanged with the original filename.
>
> Capture the return value and copy it back to jd->dir when dirname()
> returns a different pointer.
>
> Fixes: 9b07e27f88b9 ("perf inject: Add jitdump mmap injection support")
> Reported-by: sashiko-bot <[email protected]>
> Cc: Stephane Eranian <[email protected]>
> Assisted-by: Claude:claude-opus-4.6
> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
> ---
>  tools/perf/util/jitdump.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
> index 3085091b95a517ae..02840dbf8a1fc16c 100644
> --- a/tools/perf/util/jitdump.c
> +++ b/tools/perf/util/jitdump.c
> @@ -146,6 +146,7 @@ jit_open(struct jit_buf_desc *jd, const char *name)
>         ssize_t bs, bsz = 0;
>         void *n, *buf = NULL;
>         int ret, retval = -1;
> +       char *dname;
>
>         nsinfo__mountns_enter(jd->nsi, &nsc);
>         jd->in = fopen(name, "r");
> @@ -241,7 +242,9 @@ jit_open(struct jit_buf_desc *jd, const char *name)
>          */
>         strncpy(jd->dir, name, PATH_MAX - 1);
>         jd->dir[PATH_MAX - 1] = '\0';
> -       dirname(jd->dir);
> +       dname = dirname(jd->dir);
> +       if (dname != jd->dir)
> +               strlcpy(jd->dir, dname, PATH_MAX);

nit: prefer "sizeof(jd->dir)" over PATH_MAX.

Reviewed-by: Ian Rogers <[email protected]>

Thanks!
Ian

>         free(buf);
>
>         return 0;
> --
> 2.55.0
>