Skip to content

Commit 78a175c

Browse files
James-A-Clarknamhyung
authored andcommitted
perf symbol: Fix uninitialized return value in symbols__find_by_name()
found_idx and s aren't initialized, so if no symbol is found then the assert at the end will index off the end of the array causing a segfault. The function also doesn't return NULL when the symbol isn't found even if the assert passes. Fix it by initializing the values and only setting them when something is found. Fixes the following test failure: $ perf test 1 1: vmlinux symtab matches kallsyms : FAILED! Fixes: 259dce9 ("perf symbol: Remove symbol_name_rb_node") Signed-off-by: James Clark <[email protected]> Acked-by: Ian Rogers <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 2aefb4c commit 78a175c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

tools/perf/util/symbol.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,10 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[],
495495
size_t *found_idx)
496496
{
497497
size_t i, lower = 0, upper = symbols_len;
498-
struct symbol *s;
498+
struct symbol *s = NULL;
499+
500+
if (found_idx)
501+
*found_idx = SIZE_MAX;
499502

500503
if (!symbols_len)
501504
return NULL;
@@ -504,8 +507,7 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[],
504507
int cmp;
505508

506509
i = (lower + upper) / 2;
507-
s = symbols[i];
508-
cmp = symbol__match_symbol_name(s->name, name, includes);
510+
cmp = symbol__match_symbol_name(symbols[i]->name, name, includes);
509511

510512
if (cmp > 0)
511513
upper = i;
@@ -514,24 +516,24 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[],
514516
else {
515517
if (found_idx)
516518
*found_idx = i;
519+
s = symbols[i];
517520
break;
518521
}
519522
}
520-
if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) {
523+
if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) {
521524
/* return first symbol that has same name (if any) */
522525
for (; i > 0; i--) {
523526
struct symbol *tmp = symbols[i - 1];
524527

525528
if (!arch__compare_symbol_names(tmp->name, s->name)) {
526529
if (found_idx)
527530
*found_idx = i - 1;
531+
s = tmp;
528532
} else
529533
break;
530-
531-
s = tmp;
532534
}
533535
}
534-
assert(!found_idx || s == symbols[*found_idx]);
536+
assert(!found_idx || !s || s == symbols[*found_idx]);
535537
return s;
536538
}
537539

0 commit comments

Comments
 (0)