Received: by 2002:a05:7412:9c07:b0:fa:6e18:a558 with SMTP id lr7csp782073rdb; Sun, 28 Jan 2024 08:41:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IEbvrVWfQ3LnqGPKU9Jegc0xIGIKvv7v3HxC8dwyJqGgnbvEVkxverYGReMc10HoPczGHIJ X-Received: by 2002:a2e:b8c4:0:b0:2cd:3b6:b3d7 with SMTP id s4-20020a2eb8c4000000b002cd03b6b3d7mr2471213ljp.34.1706460116249; Sun, 28 Jan 2024 08:41:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706460116; cv=pass; d=google.com; s=arc-20160816; b=Syd/qHV7q8ixvq68mZLychRkfciJz0/LUyzTUVoo+s6kHCV6qtq+ySzrGos1k+hFEb lQDNqsdy2gimJr3F/gfdl2EYCGSiMSWtgrPum+fBb0J8Fj+E9aFSjFuTwpJ0h2EvIUs4 DxNDdrB6RpGhbKK1yo8PPkPJ9PUpDAQpjrk9L9QywZE4eSzgHsCW5yxn2+a5upRVDIOE jhxcy5WiFF2xePzanVdsrmobUHxJYZLj9t1ieNhtut3vgCbYnr1N73QWOLxexG3KwjyW ge5a9I2KEGi4BP7fgW/qGIIQuzkmnh9crxOM24ukOlaurfTLg9Y3z3GhaG9TOLR48t4Q QhVw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ILSTdkUYBWtFyELxhNtUPgNWTI2m6z+h87bIjVxtWjI=; fh=5hXXDaBPjy7YN/tsgRflPg9G4fl8yBY9UbfvshetmnY=; b=xeP5k69JfuXYLDdgunNPu1xB9EPLXRA884raQpSAWSvAhgHR86UHw0pwZzBD1mbWu3 EKVX/Dqz8n/TZgl/Z+MXdXJqArz0CrgGHGikRMeR1oqPQJFw89f7blMcWkO9wltkHLTl 9tVzT+JwX01hU24uJlVo+XlZYjSCp/wEu7JfMZVa+Fw/F0cF1vErXojgGktKNfYImJtU 83zNgIA56GJX95ZNLllL3hORsm52NtW9qCDMNX5Q2Hv3fkmfuQMFLGOzygCsYHCzJEr4 6xw8j6kfCqEIbHyOKM39gVOq1v/3JPoybcNSAbsZY5Yebq4woaAcpNSOu5r+blh/qmKw j2Eg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=P7X0zhEW; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41770-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41770-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 o18-20020aa7dd52000000b0055f0025e421si81246edw.596.2024.01.28.08.41.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 08:41:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41770-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=P7X0zhEW; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-41770-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41770-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 019601F23F76 for ; Sun, 28 Jan 2024 16:35:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 637D876905; Sun, 28 Jan 2024 16:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="P7X0zhEW" 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 8C4777CF0B; Sun, 28 Jan 2024 16:14:51 +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=1706458491; cv=none; b=nlU0jGu2tNbMhAXN161g/l/i9JiX7Ryj4Ap8KSdL6TcPH3OpfXWwBrpfR8U+CqTvMKDjiL1oPsn63F+CpLmgLwmSvzp3l4G5LJ3l0gnsKOSW7vTkPhs5sxXRcUczhqnDT4fcPhY911mBm7TzMJXQlBYdktIYzZs+k3K/QWX1fFc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706458491; c=relaxed/simple; bh=IYwW7e4E9Y2kJYiXwrhyvYQ/jZmbm9wx+dLNWmt5YQ8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DYPaAEG0r9LE9Kld/Ck1A+C8Q3UP99uxD5lRyAF6PFWXTRphU5pwsSLQXgMsR+5BSGFSQ4dVHcerIY3AAktPinRyAL5y3TJjD6d78N/ofN30RR5ROb/DsiIkW+DQXkcW30YzU4WUcf64zzQpECnuiToU8snO+xuok02gkzrWfnU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P7X0zhEW; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AB59C433F1; Sun, 28 Jan 2024 16:14:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706458491; bh=IYwW7e4E9Y2kJYiXwrhyvYQ/jZmbm9wx+dLNWmt5YQ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P7X0zhEWelBtfX+urwIa5WtdvVNgGHcoiR6eWBxknQOFuPFjGYJoD5QbFx/hFRb4+ FotMQ3yvTw4aMWoweGxDoIvJwIV0bzlzY3hXg7x1SDqqz6sgMjtvgKl8i9JVokob6D 9rKtNvAhx8/T6Qal3rku55lOmcqhBsY6twppCWpCJ2lUWSgGzm6upOG+ftpB5m1fJe uZoTp8PxJQfpT3JMa9poQL7IHVDJj3+/Y2NAm5k1qnOkzfWSh7pPPiuVnGsbwQZdS9 SBqJj1qoTrwzYLA8sUgyFVTI7NlOXYBQVbrF3IZ3fRVS3+Nc3netHy5L5kFr/NxSqJ 6x3n+ms1EjURA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ian Rogers , Adrian Hunter , Alexander Shishkin , Chenyuan Mi , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH AUTOSEL 6.1 15/27] libsubcmd: Fix memory leak in uniq() Date: Sun, 28 Jan 2024 11:14:00 -0500 Message-ID: <20240128161424.203600-15-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240128161424.203600-1-sashal@kernel.org> References: <20240128161424.203600-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.75 Content-Transfer-Encoding: 8bit From: Ian Rogers [ Upstream commit ad30469a841b50dbb541df4d6971d891f703c297 ] uniq() will write one command name over another causing the overwritten string to be leaked. Fix by doing a pass that removes duplicates and a second that removes the holes. Signed-off-by: Ian Rogers Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Chenyuan Mi Cc: Ingo Molnar Cc: Jiri Olsa Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: https://lore.kernel.org/r/20231208000515.1693746-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/lib/subcmd/help.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/lib/subcmd/help.c b/tools/lib/subcmd/help.c index bf02d62a3b2b..42f57b640f11 100644 --- a/tools/lib/subcmd/help.c +++ b/tools/lib/subcmd/help.c @@ -50,11 +50,21 @@ void uniq(struct cmdnames *cmds) if (!cmds->cnt) return; - for (i = j = 1; i < cmds->cnt; i++) - if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name)) - cmds->names[j++] = cmds->names[i]; - + for (i = 1; i < cmds->cnt; i++) { + if (!strcmp(cmds->names[i]->name, cmds->names[i-1]->name)) + zfree(&cmds->names[i - 1]); + } + for (i = 0, j = 0; i < cmds->cnt; i++) { + if (cmds->names[i]) { + if (i == j) + j++; + else + cmds->names[j++] = cmds->names[i]; + } + } cmds->cnt = j; + while (j < i) + cmds->names[j++] = NULL; } void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) -- 2.43.0