From: Ian Rogers Date: Sat, 17 Jan 2026 05:28:31 +0000 (-0800) Subject: perf libdw_addr2line: Fixes to srcline memory allocation X-Git-Tag: v7.0-rc1~16^2~187 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6cc3e0f659b890cfb4a8753eb0e31c871cc7555b;p=thirdparty%2Flinux.git perf libdw_addr2line: Fixes to srcline memory allocation Some irregular stack traces are causing double frees and memory leaks. Make the code robust by proactively freeing and being more careful with the memory management of the leaf_srcline. Fixes: 88c51002d06f9a68 ("perf addr2line: Add a libdw implementation") Signed-off-by: Ian Rogers Cc: Aditya Bodkhe Cc: Adrian Hunter Cc: Albert Ou Cc: Alexandre Ghiti Cc: Andi Kleen Cc: Athira Rajeev Cc: Chun-Tse Shao Cc: Dmitriy Vyukov Cc: Dr. David Alan Gilbert Cc: Guo Ren Cc: Haibo Xu Cc: Howard Chu Cc: Ian Rogers Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: John Garry Cc: Krzysztof Ɓopatowski Cc: Leo Yan Cc: Mark Wielaard Cc: Namhyung Kim Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Peter Zijlstra Cc: Sergei Trofimovich Cc: Shimin Guo Cc: Stephen Brennan Cc: Thomas Falcon Cc: Will Deacon Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/util/libdw.c b/tools/perf/util/libdw.c index e4bfd52bd172..b96c4e0d728f 100644 --- a/tools/perf/util/libdw.c +++ b/tools/perf/util/libdw.c @@ -42,16 +42,24 @@ static int libdw_a2l_cb(Dwarf_Die *die, void *_args) call_srcline = srcline_from_fileline(call_fname, die_get_call_lineno(die)); list_for_each_entry(ilist, &args->node->val, list) { + if (args->leaf_srcline == ilist->srcline) + args->leaf_srcline_used = false; + else if (ilist->srcline != srcline__unknown) + free(ilist->srcline); ilist->srcline = call_srcline; call_srcline = NULL; break; } - if (call_srcline && call_fname) + if (call_srcline && call_srcline != srcline__unknown) free(call_srcline); /* Add this symbol to the chain as the leaf. */ - inline_list__append_tail(inline_sym, args->leaf_srcline, args->node); - args->leaf_srcline_used = true; + if (!args->leaf_srcline_used) { + inline_list__append_tail(inline_sym, args->leaf_srcline, args->node); + args->leaf_srcline_used = true; + } else { + inline_list__append_tail(inline_sym, strdup(args->leaf_srcline), args->node); + } return 0; }