From nobody Sun Feb 8 13:09:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FB8EEB64DA for ; Fri, 30 Jun 2023 15:39:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232267AbjF3PjS (ORCPT ); Fri, 30 Jun 2023 11:39:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232503AbjF3PjN (ORCPT ); Fri, 30 Jun 2023 11:39:13 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0C84B10F8; Fri, 30 Jun 2023 08:39:12 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4201CD75; Fri, 30 Jun 2023 08:39:55 -0700 (PDT) Received: from e127643.broadband (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 11A2C3F73F; Fri, 30 Jun 2023 08:39:09 -0700 (PDT) From: James Clark To: linux-perf-users@vger.kernel.org, irogers@google.com Cc: James Clark , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , linux-kernel@vger.kernel.org Subject: [PATCH] perf symbol: Fix uninitialized return value in symbols__find_by_name() Date: Fri, 30 Jun 2023 16:38:39 +0100 Message-Id: <20230630153840.858668-1-james.clark@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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: 259dce914e93 ("perf symbol: Remove symbol_name_rb_node") Signed-off-by: James Clark Acked-by: Ian Rogers --- tools/perf/util/symbol.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index bc79291b9f3b..f849f9ef68e6 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -495,7 +495,10 @@ static struct symbol *symbols__find_by_name(struct sym= bol *symbols[], size_t *found_idx) { size_t i, lower =3D 0, upper =3D symbols_len; - struct symbol *s; + struct symbol *s =3D NULL; + + if (found_idx) + *found_idx =3D SIZE_MAX; =20 if (!symbols_len) return NULL; @@ -504,8 +507,7 @@ static struct symbol *symbols__find_by_name(struct symb= ol *symbols[], int cmp; =20 i =3D (lower + upper) / 2; - s =3D symbols[i]; - cmp =3D symbol__match_symbol_name(s->name, name, includes); + cmp =3D symbol__match_symbol_name(symbols[i]->name, name, includes); =20 if (cmp > 0) upper =3D i; @@ -514,10 +516,11 @@ static struct symbol *symbols__find_by_name(struct sy= mbol *symbols[], else { if (found_idx) *found_idx =3D i; + s =3D symbols[i]; break; } } - if (includes !=3D SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) { + if (s && includes !=3D SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) { /* return first symbol that has same name (if any) */ for (; i > 0; i--) { struct symbol *tmp =3D symbols[i - 1]; @@ -525,13 +528,12 @@ static struct symbol *symbols__find_by_name(struct sy= mbol *symbols[], if (!arch__compare_symbol_names(tmp->name, s->name)) { if (found_idx) *found_idx =3D i - 1; + s =3D tmp; } else break; - - s =3D tmp; } } - assert(!found_idx || s =3D=3D symbols[*found_idx]); + assert(!found_idx || !s || s =3D=3D symbols[*found_idx]); return s; } =20 --=20 2.34.1