Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22520527rwd; Fri, 30 Jun 2023 09:01:58 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4cAMbX6GOppXMU8rQyKK4XPqNFiOAIniS5q6T70loyqtPhD1Ikb44eZ8tYiTNpoK+izR9s X-Received: by 2002:a05:6a20:bc88:b0:12c:147a:face with SMTP id fx8-20020a056a20bc8800b0012c147afacemr2171063pzb.29.1688140918529; Fri, 30 Jun 2023 09:01:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688140918; cv=none; d=google.com; s=arc-20160816; b=pV5ZX3FpgKzTtJifjkh8yjDItSrn/FrdJBahkoW868+LN4zk8O3GJrmFeHwPL/2jIR +QtVtx5lnOVzrbu2dbsl/36TEkNg+aFWpX40CfScabXD6EgBcHADQY1ZAKFdi6NdSw6n rS7xYfXHajbOl1rxpF35TGKbdxxo36r/CRNW5frM4tKQsCfOPNdny+rf/JhOe60K88v0 FYHoiSoD+T/vyNufD1CEFeIl9izUMU+ely1gO0oshjv8Zga4nTQ8pG/OCh/6Gkh+FIam v6UYJE03cIiP7jQSb/ZTMsDhPs3xnXcXzqoc2ochedig1OrdaSMYV5G4VQ3ZIhQsEhx6 yohg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=YUy0+zluy0h6qyN6iVk+fr+UGcOEfrV0ezeR+UdAczo=; fh=6G11ZSykRjVXDT3BTDtd54gDGeUvSLvr1tuSgrs7/vQ=; b=RiwbhyKypyvy72V3Jfu+wcifvldjQu1O/3sxvHRpGPzuNFsFXFhg14vCCZmY4Srhap 9vtQNKDFqW3YVrqsTcYH/r+jPaDH6IoQy+WMHXa5dz82vRCaKuQhNttLtZxB6PY8sjWt 4cwyGjZLCLfQu95J53386rGoorpwhic6MQsuYpTSSKaoOliZFGMYGW2/wfsrEpdxDaVW IgAYtLfrpvkli/oWA9SrBZV+O6NtmlBBDBjgYPGBtamyjLG/68wfZe0PVyKhue0Bk5SO 05QrvqiFio6YB57KpCotUO62LShG6RSjClpVTZt0/4LX8mwQWvaZ2JWWMiCWTiT8oK6+ nWhA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id eg27-20020a056a00801b00b006768627bc5asi9928787pfb.199.2023.06.30.09.01.42; Fri, 30 Jun 2023 09:01:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232287AbjF3PjR (ORCPT + 99 others); Fri, 30 Jun 2023 11:39:17 -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: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 symbol *symbols[], size_t *found_idx) { size_t i, lower = 0, upper = symbols_len; - struct symbol *s; + struct symbol *s = NULL; + + if (found_idx) + *found_idx = SIZE_MAX; if (!symbols_len) return NULL; @@ -504,8 +507,7 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[], int cmp; i = (lower + upper) / 2; - s = symbols[i]; - cmp = symbol__match_symbol_name(s->name, name, includes); + cmp = symbol__match_symbol_name(symbols[i]->name, name, includes); if (cmp > 0) upper = i; @@ -514,10 +516,11 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[], else { if (found_idx) *found_idx = i; + s = symbols[i]; break; } } - if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) { + if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) { /* return first symbol that has same name (if any) */ for (; i > 0; i--) { struct symbol *tmp = symbols[i - 1]; @@ -525,13 +528,12 @@ static struct symbol *symbols__find_by_name(struct symbol *symbols[], if (!arch__compare_symbol_names(tmp->name, s->name)) { if (found_idx) *found_idx = i - 1; + s = tmp; } else break; - - s = tmp; } } - assert(!found_idx || s == symbols[*found_idx]); + assert(!found_idx || !s || s == symbols[*found_idx]); return s; } -- 2.34.1