From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078624; b=Vk3vF1aXKoOKZz8xZN4IGLxH+wgJKS+ekHfzctjJUoc8KDEMrn/aev7GMBEJAxpw9h6H 282MVXGsij8Gk0mcOrmnjkQmtXhwBKSzOFIwRrgF1l313v3LO7hR/+7WbSKEorQ+fbf0m s314JYRYpIhhwaxapMDKRI6m2lcYJuMBmEHOE7bzw4nCKugFO8ec7ut9tT830X4fNNUnr x2Y2SxTrvx0s57SuGoEz7X4/sJs4vXbMSutDqF6fkAHBNgT8ZXDL2nx50E7/8Y862/kEV iwMxBQMYN6lIFy9MvUITzu2+Q8Ed6x/xYEYb7G3XXTc4yG659M4/+zSZL7tvcTyHKyA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078624; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=P/br0672bHkupaAEiREmpsGoMNUwYK+VZ3J5rCdXt2s=; b=27FCIHtNNBVrWxXOjd2Ky6FTtB2lTNEMpF6G4IShLJ1Q4JcOVl8J2CItz29e6DYcWpnn x3ECQboWpEcmVvXUkT7tYmr043DfyszGSfrax7e1HOE/YFg6uXPoEPr6/mRbbgGC7KkL/ 212JKCdL11vgshCYSzRAOu8Xsn45YJu777Mz16L9eXH4NDXRe6XBrgeHQ1aeuqVpbp/F0 KpsD0DlHacBHBSoHq2gFY18SHSgY13mpxKSpEDktgP2XC+eVT96B2ZyyEN4GNvpyseEWG 15fbtDgQQZqhBHhXvACtADkyxOTlOuVydUF1M2X5s3pqSZ1Cg98xeRPFFz5HM4PW7+w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=EPvkXsU7; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94331606A691 for ; Mon, 30 Nov 2026 22:43:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 94331606A691 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=EPvkXsU7 Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-3869accaac2so7073975eec.1 for ; Mon, 30 Nov 2026 14:43:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796078624; x=1796683424; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=P/br0672bHkupaAEiREmpsGoMNUwYK+VZ3J5rCdXt2s=; b=EPvkXsU7Zb6i85YzUFDK/+AQxWN4rNqKKkF5YbgEuHUAGZR+IAkmfc1PXHFvG/X/PN WYZtZuCDGJx1Sz4ewRMn/GszvzmoLIhzVFdwHheKSYBiPVR1AzELxzAnlCP4r+HLamRf x3+q6+S6/Q3QIBydM37rltxZzDY1yjky5f8tVoM7BVli0g5CpgMcmQuaGaLCpEwsqBX+ xluYzUbtQ5rLQwxppk3TWkAag0m8ZvsiXACYexSTrf1YGAcKgqGUzxR0t57cPSxLjkX2 4VQ97WmnHeCOuUQyNZ5dbSRk/OMM04db/KjgKZl4alrSHwVOSm23sc4gIrWD5YJ26HpW 4trg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796078624; x=1796683424; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=P/br0672bHkupaAEiREmpsGoMNUwYK+VZ3J5rCdXt2s=; b=bk1IV9AjfmhyCujApkG//7XWO1DuaV5tRi5yYL7zC6SBLPLx3PIdYHF0I0DvHa0cwj LJBFnc3s1p8pGIb0HonCcuJJ+k/I+VZmXHz5TjueyOA9vKyz2Xo0L6nhVdOuL3ai4lE/ RiLqpC+qzKkB1WOee/u4rmtPdPqBRmAX9t0Qit4NIYQGeMBimpThR+T2R8CshZmQBba6 Ey6446S7pZaq3jdW9CEE3jUE/1YAwujGNix9OfKzJu4+SKN2JdjI52G93YhLVnH6e0gO VDCwANkD70BxPYrzW4XMsrRFrWFFokp7sAYxaT6/L2ge/EQe01BpYMmKs0SDc5aOhYim rO6g== X-Forwarded-Encrypted: i=1; AL+hErEwLyfet5UvXodN+QTVS18qk9Ncx3u7Xz1ChcY0uFoVExqwq+IvQY2xHXJatTXew+EveEyOkwM1a+dxM1qTQWW0@vger.kernel.org X-Gm-Message-State: APuU4Z/tB4CmOBZGqpc02iQVIoAqZtoOd+dAWTGg+3dzO1Zw/uzDWukk daCWhaDRGeX6tfwh6tluw1kA5s5QIDnF5dqxuV//mW43QJdGXCPX6LXnTnXx6U9DtvP88lbe/cQ sG5tZ/4UoDw== X-Received: from dycmb5-n2.prod.google.com ([2002:a05:693c:20c5:20b0:369:fc78:c914]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:d18b:b0:386:b4f1:dab0 with SMTP id 5a478bee46e88-386b4f1e04fmr26151100eec.1.1796078623290; Mon, 30 Nov 2026 14:43:43 -0800 (PST) Date: Mon, 30 Nov 2026 14:43:31 -0800 In-Reply-To: <20261130224332.79575-1-irogers@google.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130224332.79575-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130224332.79575-4-irogers@google.com> Subject: [PATCH v2 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Jiri Olsa , Adrian Hunter , James Clark , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" In resolve_metric(), growing the pending array assigns the result of realloc() directly back to pending; if realloc() fails after earlier iterations succeeded, the existing pending allocation is overwritten with NULL and leaked. Similarly, in __add_metric(), assigning realloc() directly to root_metric->metric_refs overwrites the existing pointer with NULL on failure, preventing metric__free(root_metric) in the root caller from freeing the prior metric_refs array. Use temporary pointers for both realloc() calls, freeing pending before returning -ENOMEM in resolve_metric() and preserving root_metric->metric_refs for metric__free() in __add_metric(). Fixes: 80be6434c36f ("perf metric: Modify resolution and recursion check") Fixes: 46bdc0bf8d21 ("perf metric: Simplify metric_refs calculation") Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 779fe56fe8d0..489f13b34361 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -771,10 +771,15 @@ static int resolve_metric(struct list_head *metric_list, if (pmu_metrics_table__find_metric(table, pmu, cur->pkey, metricgroup__find_metric_callback, &pm) != PMU_METRICS__NOT_FOUND) { - pending = realloc(pending, - (pending_cnt + 1) * sizeof(struct to_resolve)); - if (!pending) + struct to_resolve *new_pending; + + new_pending = realloc(pending, + (pending_cnt + 1) * sizeof(struct to_resolve)); + if (!new_pending) { + free(pending); return -ENOMEM; + } + pending = new_pending; memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm)); pending[pending_cnt].key = cur->pkey; @@ -863,6 +868,7 @@ static int __add_metric(struct list_head *metric_list, return -ENOMEM; } else { + struct metric_ref *new_refs; int cnt = 0; /* @@ -879,10 +885,11 @@ static int __add_metric(struct list_head *metric_list, } /* Create reference. Need space for the entry and the terminator. */ - root_metric->metric_refs = realloc(root_metric->metric_refs, - (cnt + 2) * sizeof(struct metric_ref)); - if (!root_metric->metric_refs) + new_refs = realloc(root_metric->metric_refs, + (cnt + 2) * sizeof(struct metric_ref)); + if (!new_refs) return -ENOMEM; + root_metric->metric_refs = new_refs; /* * Intentionally passing just const char pointers, -- 2.56.1.839.g22a8290db9-goog