All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Suchit Karunakaran <suchitkarunakaran@gmail.com>,
	mingo@redhat.com, namhyung@kernel.org, peterz@infradead.org,
	adrian.hunter@intel.com, alexander.shishkin@linux.intel.com,
	james.clark@linaro.org, jolsa@kernel.org, mark.rutland@arm.com,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] perf annotate: Fix memcpy size in arch__grow_instructions
Date: Fri, 23 Jan 2026 14:26:43 -0300	[thread overview]
Message-ID: <aXOvUzI56ePokDd_@x1> (raw)
In-Reply-To: <CAP-5=fVOeYCU_6ukDi_3g=H=K5HXoN+UjcNCysEbCO3LAY3ukw@mail.gmail.com>

On Thu, Jan 22, 2026 at 10:27:27AM -0800, Ian Rogers wrote:
> On Thu, Jan 22, 2026 at 9:17 AM Suchit Karunakaran <suchitkarunakaran@gmail.com> wrote:

> > The memcpy in arch__grow_instructions() is copying the wrong number of
> > bytes when growing from a non-allocated table. It should copy
> > arch->nr_instructions * sizeof(struct ins) bytes, not just
> > arch->nr_instructions bytes.

> > This bug causes data corruption as only a partial copy of the
> > instruction table is made, leading to garbage data in most entries and
> > potential crashes

> > Signed-off-by: Suchit Karunakaran <suchitkarunakaran@gmail.com>
> > ---
> >  tools/perf/util/disasm.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)

> > diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> > index 50b9433f3f8e..6faa9df8e373 100644
> > --- a/tools/perf/util/disasm.c
> > +++ b/tools/perf/util/disasm.c
> > @@ -81,7 +81,7 @@ static int arch__grow_instructions(struct arch *arch)
> >         if (new_instructions == NULL)
> >                 return -1;

> > -       memcpy(new_instructions, arch->instructions, arch->nr_instructions);
> > +       memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(struct ins));

> Reviewed-by: Ian Rogers <irogers@google.com>

> Nice catch! I think we've been trying to make code like this more like:
> memcpy(new_instructions, arch->instructions, arch->nr_instructions *
> sizeof(*new_instructions));
> but the immediately preceding calloc isn't doing this, so your patch
> is consistent. There is also a clean up in this code to switch to
> using reallocarray rather than realloc.

Right, that would be better, but the fix is good as is and we can
cleanup it later.

I added the missing:

Fixes: 2a1ff812c40be982 ("perf annotate: Introduce alternative method of keeping instructions table")

Its an old bug I introduced almost 10 years ago ;-\

- Arnaldo

      reply	other threads:[~2026-01-23 17:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 17:17 [PATCH] perf annotate: Fix memcpy size in arch__grow_instructions Suchit Karunakaran
2026-01-22 18:27 ` Ian Rogers
2026-01-23 17:26   ` Arnaldo Carvalho de Melo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aXOvUzI56ePokDd_@x1 \
    --to=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=suchitkarunakaran@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.