[PATCH v3 3/6] perf libdw: Fix libdw API contract violations

Ian Rogers
2026-05-03T00:36:03Z

Check return values of `dwfl_report_end` and `dwfl_module_addrdie`.
Validate `dwarf_diename` result before passing to `new_inline_sym` (avoid
potential `strdup(NULL)` crash) and check `die_get_call_lineno` for
errors.

Fixes: b7a2b011e962 ("perf powerpc: Unify the skip-callchain-idx libdw with that for addr2line")
Fixes: 88c51002d06f ("perf addr2line: Add a libdw implementation")
Assisted-by: Gemini-CLI:Google Gemini 3
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/libdw.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/libdw.c b/tools/perf/util/libdw.c
index 216977884103..e37f3b22699d 100644
--- a/tools/perf/util/libdw.c
+++ b/tools/perf/util/libdw.c
@@ -60,7 +60,11 @@ struct Dwfl *dso__libdw_dwfl(struct dso *dso)
return NULL;
}

- dwfl_report_end(dwfl, /*removed=*/NULL, /*arg=*/NULL);
+ if (dwfl_report_end(dwfl, NULL, NULL) != 0) {
+ dwfl_end(dwfl);
+ return NULL;
+ }
+
dso__set_libdw(dso, dwfl);

return dwfl;
@@ -72,22 +76,27 @@ struct libdw_a2l_cb_args {
struct inline_node *node;
char *leaf_srcline;
bool leaf_srcline_used;
+ int err;
};

static int libdw_a2l_cb(Dwarf_Die *die, void *_args)
{
struct libdw_a2l_cb_args *args = _args;
- struct symbol *inline_sym = new_inline_sym(args->dso, args->sym, dwarf_diename(die));
+ const char *name = dwarf_diename(die);
+ struct symbol *inline_sym = new_inline_sym(args->dso, args->sym, name ?: "unknown");
const char *call_fname = die_get_call_file(die);
+ int call_lineno = die_get_call_lineno(die);
char *call_srcline = srcline__unknown;
struct inline_list *ilist;

- if (!inline_sym)
- return -ENOMEM;
+ if (!inline_sym) {
+ args->err = -ENOMEM;
+ return DWARF_CB_ABORT;
+ }

/* Assign caller information to the parent. */
- if (call_fname)
- call_srcline = srcline_from_fileline(call_fname, die_get_call_lineno(die));
+ if (call_fname && call_lineno > 0)
+ call_srcline = srcline_from_fileline(call_fname, call_lineno);

list_for_each_entry(ilist, &args->node->val, list) {
if (args->leaf_srcline == ilist->srcline)
@@ -163,7 +172,8 @@ int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,
};

/* Walk from the parent down to the leaf. */
- cu_walk_functions_at(cudie, addr, libdw_a2l_cb, &args);
+ if (cudie)
+ cu_walk_functions_at(cudie, addr, libdw_a2l_cb, &args);

if (!args.leaf_srcline_used)
free(args.leaf_srcline);
--
2.54.0.545.g6539524ca2-goog