Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp267388lqh; Mon, 6 May 2024 20:08:25 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXH9FRI/THXFC8B5mgp2c6XnPOANO/jTgux/bt6eDALkZ/t81oAAZA1CDFOL8UTc7oITQAFnewIHHOdxhnNKVYC/Yv7hmo2sI2VdaKTbg== X-Google-Smtp-Source: AGHT+IHwkeLxYsWF3xUDjQLBR0ps9ZkMewyo+4KdYHoAV9T/FDnFmbyM7j7kdm54nx8K0nxjPgsT X-Received: by 2002:a50:8e01:0:b0:572:a172:be71 with SMTP id 1-20020a508e01000000b00572a172be71mr1174978edw.14.1715051305497; Mon, 06 May 2024 20:08:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715051305; cv=pass; d=google.com; s=arc-20160816; b=j1A5nygaPQI2dSCIsgR0kIgNahupaMC6N3xbGUBKUWrFwQB/IOydkB4oh4ufZrI454 Ls8pVHOWTyGcYpjVgY+DGrb4PY5PP7xxV/vGSxQfZwMabBhOAR2GfylMLd9jw+E5RZI9 yYk8a7ykDp1w+BeDo3Zdpam/x5y1v4xf0muOleanXTp9eHqMaOyIwv013VKx0RDbjfEz /XKaJjM6AFAmZxRIaoWYSCl5q3kG4Yz2EZD9et13mo7bMbt2agSEqtH0zcUjaoNycALs /PGBsqR87pGZuEn+rcGNn3mlPPPaBDuXEBsS4b2m8nl3VNbl0c7oHHtvgl2VrIFN6y2J 0obQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date :dkim-signature; bh=/Sf0Jb12kqDXM17pqkIhWA2WU64bWd34wsOKZAaGdgQ=; fh=is6QEHU4WBs69coe4TttkSXZQVGNOWql+xxG8P9D+7E=; b=uJ2hH1jrCOFUEZALoK/kR/HzNYPm4KCq+aoIwNtm3qojxvSS8ePej+8jWDfdtMsujn HbmKyNqOFHYCPLphNeVJBJqCegq1tmhcJZZeWXJK+yp2AeEu+IXjyD0i8Aui+XxsY6dK fXcc+ZdCWmIV0ccM5wlhj8StUTkNYgIGH8Z1+bGkbPJDq1HZE1uHas5VPNAnr0g2/nId Hf+Gi3TmiVR6T2b9rMmPvfIGUMbIP7H/5nlTA9FxRDpqzODHO09uOqvNOadi6FrX1DE7 Ddm2m796/n6+qzs7BjnJUjTTMsl+8w7bLpE3jWGU+rG7esBs7ipQcZIUnA9ucJH2CWu0 KM6w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="N/ncxEHB"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-170604-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170604-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z21-20020a056402275500b005726c8689a1si5677293edd.211.2024.05.06.20.08.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 20:08:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170604-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="N/ncxEHB"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-170604-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170604-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 14D8E1F22873 for ; Tue, 7 May 2024 03:08:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 173C43FBB0; Tue, 7 May 2024 03:08:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="N/ncxEHB" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C44C441D; Tue, 7 May 2024 03:08:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715051296; cv=none; b=oMX0K6xPkUGLD2uOQS+a4DWKo8lFwaZa4P1MmdIsKVr8Iz2YwnZ2yDYxeJGeX40m7iTX2cj3+1qIA6GQyJNqlWw8S4ocUeFCE7T2TVGisOi2eIAvqaCF3Te+aBbV2Ylyrr7uM+UJ1GAS5kKhMMo4x2W6/QX9PsuVCZDrZTQEpQQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715051296; c=relaxed/simple; bh=sjD21oc5mAlByVhCwT3GATvzWe4pbEF4vX35kLIucIc=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=n21nyoJsskaRkPnJdzRtW3AkGT69+Dfp0YYfR2b57iRbMVwrMD1EnQanGHKj23LaYqk1u0Zi9ublJzXzoEaAZ1jSXDQl9gNvqTbLFnYmfpyizz/Ci1zMarfrx//GWjVAHkjNkbbkn3OcWSdPK+9WhHDg0p8DNhwu/zldcRzQzPY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=N/ncxEHB; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 319B6C116B1; Tue, 7 May 2024 03:08:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715051295; bh=sjD21oc5mAlByVhCwT3GATvzWe4pbEF4vX35kLIucIc=; h=Date:From:To:Cc:Subject:From; b=N/ncxEHBNBX9Dga6tJLHr7hD+N2uTpOfuRj6UQzND6c6XsM4p1apAQLrMHEvPf0b/ VDJS6bt7ii89PLs26kUI5/ZyWkYFvBH6fk/CtyyarR14m1Bm159m99qUaF823GHbZu BrkFoDs7X6g3sBizA8YRLUvzhdcHxh657hye+f5e2+82fLtw7P2k3W4BOTDY2Bdfrd YFSfPOyjWLzAlugqbSFtYGxkVu6PqVI+mfPpO1wH4KwOXrm21Oo3+ScHn8Z0emvF6A rJVfmlJI1Y2aQ8P5E5cDBWC8eI/p3mHyrkRqi5Ypuezaj8uzk1LjVGG2Q8nJTITnJ8 6/FP9BMdTbfvg== Date: Tue, 7 May 2024 00:08:12 -0300 From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Adrian Hunter , Ian Rogers , Jiri Olsa , Kan Liang , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 1/1] perf annotate: Use zfree() to avoid possibly accessing dangling pointers Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline When freeing a->b it is good practice to set a->b to NULL using zfree(&a->b) so that when we have a bug where a reference to a freed 'a' pointer is kept somewhere, we can more quickly cause a segfault if some code tries to use a->b. This is mostly done but some new cases were introduced recently, convert them to zfree(). Cc: Adrian Hunter Cc: Ian Rogers Cc: Jiri Olsa Cc: Kan Liang Cc: Namhyung Kim Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/ui/browsers/annotate-data.c | 3 ++- tools/perf/util/annotate-data.c | 17 +++++++++-------- tools/perf/util/annotate.c | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/tools/perf/ui/browsers/annotate-data.c b/tools/perf/ui/browsers/annotate-data.c index a4a0f042f201a35c..8d6bf08d371dfd23 100644 --- a/tools/perf/ui/browsers/annotate-data.c +++ b/tools/perf/ui/browsers/annotate-data.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include "ui/browser.h" @@ -130,7 +131,7 @@ static void annotated_data_browser__delete_entries(struct annotated_data_browser list_for_each_entry_safe(pos, tmp, &browser->entries, node) { list_del_init(&pos->node); - free(pos->hists); + zfree(&pos->hists); free(pos); } } diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index faefa444af1e0d96..57e7d4b3550b42f8 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "annotate.h" #include "annotate-data.h" @@ -311,8 +312,8 @@ static void delete_members(struct annotated_member *member) list_for_each_entry_safe(child, tmp, &member->children, node) { list_del(&child->node); delete_members(child); - free(child->type_name); - free(child->var_name); + zfree(&child->type_name); + zfree(&child->var_name); free(child); } } @@ -582,7 +583,7 @@ void global_var_type__tree_delete(struct rb_root *root) rb_erase(node, root); gvar = rb_entry(node, struct global_var_entry, node); - free(gvar->name); + zfree(&gvar->name); free(gvar); } } @@ -1817,16 +1818,16 @@ static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_en err: while (--i >= 0) - free(adt->histograms[i]); - free(adt->histograms); + zfree(&(adt->histograms[i])); + zfree(&adt->histograms); return -ENOMEM; } static void delete_data_type_histograms(struct annotated_data_type *adt) { for (int i = 0; i < adt->nr_histograms; i++) - free(adt->histograms[i]); - free(adt->histograms); + zfree(&(adt->histograms[i])); + zfree(&adt->histograms); } void annotated_data_type__tree_delete(struct rb_root *root) @@ -1840,7 +1841,7 @@ void annotated_data_type__tree_delete(struct rb_root *root) pos = rb_entry(node, struct annotated_data_type, node); delete_members(&pos->self); delete_data_type_histograms(pos); - free(pos->self.type_name); + zfree(&pos->self.type_name); free(pos); } } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index d7d55263fc91b67e..2b178835c1f3c254 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -2618,13 +2618,13 @@ static void delete_basic_blocks(struct basic_block_data *bb_data) list_for_each_entry_safe(link, tmp, &bb_data->queue, node) { list_del(&link->node); - free(link->bb); + zfree(&link->bb); free(link); } list_for_each_entry_safe(link, tmp, &bb_data->visited, node) { list_del(&link->node); - free(link->bb); + zfree(&link->bb); free(link); } } -- 2.44.0