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

From: Ian Rogers

Date: Wed Aug 05 2026 - 15:17:04 EST


On Wed, Aug 5, 2026 at 6:32 AM Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
>
> 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 <sashiko-bot@xxxxxxxxxx>
> Cc: Stephane Eranian <eranian@xxxxxxxxxx>
> Assisted-by: Claude:claude-opus-4.6
> Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> ---
> 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 <irogers@xxxxxxxxxx>

Thanks!
Ian

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