From patchwork Tue Jul 9 18:31:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaldo Carvalho de Melo X-Patchwork-Id: 168759 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp8896563ilk; Tue, 9 Jul 2019 11:32:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqzPs8HdRqRe/AbIQYfwadVGglAS7duhZSH6vjFS32SrCzojlJ5TrTWT8JYfY3PYc+2scmze X-Received: by 2002:a17:902:8d91:: with SMTP id v17mr33652668plo.91.1562697144997; Tue, 09 Jul 2019 11:32:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562697144; cv=none; d=google.com; s=arc-20160816; b=SB84A8GLSgW+0wVQTW/vXzjjS5r5eidqwNFSn6j3zKS9skMLpMZvAp8rpuh9VfQxzI Ruq8tzXpwTyAEmXnZ9bfDRsg1K9r+LOpsisvOO0+vMvVUlIzQdyuLjXdZUO2j/H+QbGc LuaGjNqMw3beJ7sV62nn10GFwwhGGwEL5871qAgeYWCHGxpF3mSyvdl5GwKqjz9Q7eeJ d8/BXDy5sT+USTbbwFOp5avSbuxxYv3ZI+ppUGHDG3jvyX6bT+moNhggKl6y2yrymOxS 2CMGmDvEjTctoaa+P/hGRWCo+sqSrZVJuvhlZ3SS0buwc4fTDHFv+c0SpY8Z6unVmjFA cVVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tT7T/TRs6aplUF1AgzCcp0Ff/DKIOnN8nESTF2du4WM=; b=ew0UjvFrJynj8d7qlDMFci1Zd17mqj53Ba7Ik6eb8LwNeJ0tMZVxejLosXRbQ3j01p kdXW6W84GG0rn8zLH4Ip1+RPMhivnCuExRGjb3ZfOn6kbJeywc4dYtLeGhQBQK+NsR7J s2y8XxWzzTEHWg2z66CHLGrxETXqFqY5P6oKt80vNG2V9DXp7LxTzMTKflA+DOFv7NAg bGQLGBjzGjKR4lqPS694OGN9KQOmXC47iGQe+GWfmMZFSsDIir/zKrznx+HZ0wnFUG0y Jyj2aKsUEvyaipKNaPEgydXxTNA7GrKvl5xRNiXzPGKj7egkcXHUjPooBVsvYnnLkV6y FVKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ESqkemMN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u185si20804361pgd.561.2019.07.09.11.32.24; Tue, 09 Jul 2019 11:32:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ESqkemMN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729125AbfGIScX (ORCPT + 30 others); Tue, 9 Jul 2019 14:32:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:53568 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729094AbfGIScV (ORCPT ); Tue, 9 Jul 2019 14:32:21 -0400 Received: from quaco.ghostprotocols.net (unknown [179.97.35.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2FE342171F; Tue, 9 Jul 2019 18:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562697136; bh=JjyDoauJ+WYz56YyYDKCZ3keOnFJy6NiR6Q0BlDn5EY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ESqkemMNPwhyxKwOQR1gPTIAR2g071vi5m0lY8XDTWTk9gxiQcipmE/K7Zan3h6h4 Hf3SBt9jwNbo5P0pUS1hf4uidv2b5RDrvxbykDHuwGO8ZT1TTc4GojtJ/VnVOuxZet b7Jb7VD996e5+fP2saq8AJCYYDH9elce/osSNPYE= From: Arnaldo Carvalho de Melo To: Ingo Molnar , Thomas Gleixner Cc: Jiri Olsa , Namhyung Kim , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Leo Yan , Adrian Hunter , Alexander Shishkin , Alexey Budankov , Alexios Zavras , Andi Kleen , Changbin Du , "David S . Miller" , Davidlohr Bueso , Eric Saint-Etienne , Jin Yao , Konstantin Khlebnikov , Mathieu Poirier , Peter Zijlstra , Rasmus Villemoes , Song Liu , Suzuki Poulouse , Thomas Richter , linux-arm-kernel@lists.infradead.org, Arnaldo Carvalho de Melo Subject: [PATCH 06/25] perf map: Fix potential NULL pointer dereference found by smatch tool Date: Tue, 9 Jul 2019 15:31:07 -0300 Message-Id: <20190709183126.30257-7-acme@kernel.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190709183126.30257-1-acme@kernel.org> References: <20190709183126.30257-1-acme@kernel.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Leo Yan Based on the following report from Smatch, fix the potential NULL pointer dereference check. tools/perf/util/map.c:479 map__fprintf_srccode() error: we previously assumed 'state' could be null (see line 466) tools/perf/util/map.c 465 /* Avoid redundant printing */ 466 if (state && 467 state->srcfile && 468 !strcmp(state->srcfile, srcfile) && 469 state->line == line) { 470 free(srcfile); 471 return 0; 472 } 473 474 srccode = find_sourceline(srcfile, line, &len); 475 if (!srccode) 476 goto out_free_line; 477 478 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode); 479 state->srcfile = srcfile; ^^^^^^^ 480 state->line = line; ^^^^^^^ This patch validates 'state' pointer before access its elements. Signed-off-by: Leo Yan Acked-by: Jiri Olsa Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Alexey Budankov Cc: Alexios Zavras Cc: Andi Kleen Cc: Changbin Du Cc: David S. Miller Cc: Davidlohr Bueso Cc: Eric Saint-Etienne Cc: Jin Yao Cc: Konstantin Khlebnikov Cc: Mathieu Poirier Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Rasmus Villemoes Cc: Song Liu Cc: Suzuki Poulouse Cc: Thomas Gleixner Cc: Thomas Richter Cc: linux-arm-kernel@lists.infradead.org Fixes: dd2e18e9ac20 ("perf tools: Support 'srccode' output") Link: http://lkml.kernel.org/r/20190702103420.27540-8-leo.yan@linaro.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/map.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) -- 2.21.0 diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index 6fce983c6115..5f87975d2562 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -476,8 +476,11 @@ int map__fprintf_srccode(struct map *map, u64 addr, goto out_free_line; ret = fprintf(fp, "|%-8d %.*s", line, len, srccode); - state->srcfile = srcfile; - state->line = line; + + if (state) { + state->srcfile = srcfile; + state->line = line; + } return ret; out_free_line: