Re: [PATCH 06/12] perf jitdump: Fix funlockfile on unlocked stream in jit_open() error path

From: Ian Rogers

Date: Wed Aug 05 2026 - 15:09:35 EST


On Wed, Aug 5, 2026 at 6:31 AM Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
>
> If the malloc() for the initial read buffer fails, jit_open() jumps to
> the error label which calls funlockfile(jd->in). However, flockfile()
> is called later in the function, so at this point the stream was never
> locked. Calling funlockfile() on an unlocked stream is undefined
> behavior per POSIX.
>
> Split the error path into two labels: 'error' (after flockfile) calls
> funlockfile before cleanup, 'error_noflock' (before flockfile) skips
> the unlock.
>
> 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>

Reviewed-by: Ian Rogers <irogers@xxxxxxxxxx>

Thanks!
Ian

> ---
> tools/perf/util/jitdump.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
> index fd11e07bf00b7978..c3f11d1c1d76d6c7 100644
> --- a/tools/perf/util/jitdump.c
> +++ b/tools/perf/util/jitdump.c
> @@ -155,7 +155,7 @@ jit_open(struct jit_buf_desc *jd, const char *name)
>
> buf = malloc(bsz);
> if (!buf)
> - goto error;
> + goto error_noflock;
>
> /*
> * protect from writer modifying the file while we are reading it
> @@ -244,8 +244,9 @@ jit_open(struct jit_buf_desc *jd, const char *name)
>
> return 0;
> error:
> - free(buf);
> funlockfile(jd->in);
> +error_noflock:
> + free(buf);
> fclose(jd->in);
> return retval;
> }
> --
> 2.55.0
>