From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Chenyuan Mi <cymi20@fudan.edu.cn>, Ingo Molnar <mingo@redhat.com>,
Jiri Olsa <jolsa@kernel.org>, Mark Rutland <mark.rutland@arm.com>,
Namhyung Kim <namhyung@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Arnaldo Carvalho de Melo <acme@redhat.com>,
Sasha Levin <sashal@kernel.org>
Subject: [PATCH AUTOSEL 5.15 12/19] libsubcmd: Fix memory leak in uniq()
Date: Sun, 28 Jan 2024 11:15:09 -0500 [thread overview]
Message-ID: <20240128161524.204182-12-sashal@kernel.org> (raw)
In-Reply-To: <20240128161524.204182-1-sashal@kernel.org>
From: Ian Rogers <irogers@google.com>
[ 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 <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Chenyuan Mi <cymi20@fudan.edu.cn>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20231208000515.1693746-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
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
next prev parent reply other threads:[~2024-01-28 16:15 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-28 16:14 [PATCH AUTOSEL 5.15 01/19] PCI: Only override AMD USB controller if required Sasha Levin
2024-01-28 16:14 ` [PATCH AUTOSEL 5.15 02/19] PCI: switchtec: Fix stdev_release() crash after surprise hot remove Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 03/19] perf cs-etm: Bump minimum OpenCSD version to ensure a bugfix is present Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 04/19] xhci: fix possible null pointer deref during xhci urb enqueue Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 05/19] usb: hub: Replace hardcoded quirk value with BIT() macro Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 06/19] selftests/sgx: Fix linker script asserts Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 07/19] tty: allow TIOCSLCKTRMIOS with CAP_CHECKPOINT_RESTORE Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 08/19] fs/kernfs/dir: obey S_ISGID Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 09/19] PCI: Fix 64GT/s effective data rate calculation Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 10/19] PCI/AER: Decode Requester ID when no error info found Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 11/19] misc: lis3lv02d_i2c: Add missing setting of the reg_ctrl callback Sasha Levin
2024-01-28 16:15 ` Sasha Levin [this message]
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 13/19] drm/amdkfd: Fix lock dependency warning Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 14/19] virtio_net: Fix "‘%d’ directive writing between 1 and 11 bytes into a region of size 10" warnings Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 15/19] blk-mq: fix IO hang from sbitmap wakeup race Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 16/19] ceph: fix deadlock or deadcode of misusing dget() Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 17/19] drm/amd/powerplay: Fix kzalloc parameter 'ATOM_Tonga_PPM_Table' in 'get_platform_power_management_table()' Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 18/19] drm/amdgpu: Release 'adev->pm.fw' before return in 'amdgpu_device_need_post()' Sasha Levin
2024-01-28 16:15 ` [PATCH AUTOSEL 5.15 19/19] drm/amdkfd: Fix 'node' NULL check in 'svm_range_get_range_boundaries()' Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240128161524.204182-12-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=acme@redhat.com \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=cymi20@fudan.edu.cn \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox