[PATCH 2/4] help.c::exclude_cmds: plug a leak

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Create a fresh cmdnames to hold the entries we want to keep, such that
we free the excluded entries in cmds only.

Signed-off-by: Tay Ray Chuan <rctay89@xxxxxxxxx>

---

A solution that does not require a fresh cmdnames escapes me.
---
 help.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/help.c b/help.c
index b64056d..705f152 100644
--- a/help.c
+++ b/help.c
@@ -65,21 +65,29 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 	int ci, cj, ei;
 	int cmp;
 
+	struct cmdnames excluded;
+	memset(&excluded, 0, sizeof(excluded));
+	ALLOC_GROW(excluded.names, cmds->cnt, excluded.alloc);
+
 	ci = cj = ei = 0;
 	while (ci < cmds->cnt && ei < excludes->cnt) {
 		cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
-		if (cmp < 0)
-			cmds->names[cj++] = cmds->names[ci++];
-		else if (cmp == 0)
+		if (cmp < 0) {
+			excluded.names[cj] = cmds->names[ci];
+			cmds->names[ci] = NULL;
+			ci++, cj++;
+		} else if (cmp == 0)
 			ci++, ei++;
 		else if (cmp > 0)
 			ei++;
 	}
 
-	while (ci < cmds->cnt)
-		cmds->names[cj++] = cmds->names[ci++];
-
+	clean_cmdnames(cmds);
+	cmds->alloc = excluded.alloc;
 	cmds->cnt = cj;
+	cmds->names = excluded.names;
+	while (cj--)
+		cmds->names[cj] = excluded.names[cj];
 }
 
 static void pretty_print_string_list(struct cmdnames *cmds,
-- 
1.7.10.1.611.g8a79d96

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]