From patchwork Thu Aug 1 10:40:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13750138 Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (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 33DDE184557 for ; Thu, 1 Aug 2024 10:40:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.148 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722508832; cv=none; b=LNNBNQ0V/9TulrDsYt+j5nTN2y1CmFcELoE1YOBgGs9f4h8k834/4iMdSIwUDUkUBsCOiNnHfcNmFkxlGczhrng8uPwYGSbbWfctwTSoC9MxUuPflrk2jA9FVAwuwAmc+lP6d/TM2Wx7k3c1+9qeHbVStj0ocuzHXjxUOh/qnQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722508832; c=relaxed/simple; bh=aD4gKYdSCRdZqHM4ldZE8WWVQqVM4nExvEMDOUej5ls=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=D7fymeIpVRNEZY2Vwv7BIKyNDPDSkzf9kQL9Anr4XAC0lsKxOUYAszIz8mTymBwVa16GOQjAr9UfDy+2uL9pX4aUAEYyIyowj+z8AMajQSe/R0spcM3uT6C4UJCn+q7Ra7HfGv7h/a42xDqP4Vjie4UCYEhO5i8yl9ekMthVpmQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=XsJMrVra; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=pqRBbSxP; arc=none smtp.client-ip=103.168.172.148 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="XsJMrVra"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="pqRBbSxP" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.nyi.internal (Postfix) with ESMTP id 8A7851388071; Thu, 1 Aug 2024 06:40:30 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Thu, 01 Aug 2024 06:40:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1722508830; x=1722595230; bh=eM2BI1+g22 fBnNfLtyVDuJyfrz8KW8hHjJ72M3hISAY=; b=XsJMrVraPpXeT2z/Av4VBlx9R8 Fp1oXeI7VmLWxtbNsnEeP9BfweIadEuajUpqaq14m772dVcTlcY5rPrVfgjFcQJe ymopG4Z4yu7d4ZM/j0+F3l7uTK75pZGJdAxgK5Zx6iXvGHQKY+BwcLlgVyToE+JD HxZWOYSCZcutCWVlQjb0Lkfg5TLf66415YFoh3BxxOxFmFvRRAiheIZEYsNa+ksQ gkdOHL9XAw7tfTT1sHkkEG3yRS3w/CcPOLXYgsyw+XdVY/tW/4iAjRFMFVD4gijq b9oCkW3ZGs8uGzgQyGdIz5EWgNjnytxvNk6V6am5yUTwyFriRJNoZHnZ4mDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1722508830; x=1722595230; bh=eM2BI1+g22fBnNfLtyVDuJyfrz8K W8hHjJ72M3hISAY=; b=pqRBbSxPgUNUTGQBXS+zI0TmGgvP/tIQJ/ErB1Z/ZUvR Aacs6KnUktQSNLJX7GnjnzrhtRxtrNABYVlkUUWyHPHG88tALNrHo8hRw5f8UJhr 39Hk8PIpN95By23OfwfbOsvGpw0Z8OxU/F9nqd3y1lG16K7PZa5MjFSh283Iz9tR 03pgvjvVWC57Fyed+jma0vakEzG4aShkSTy0HgFXtuVXWMQ9vHvfqXlmwKkp2Qm9 U8ynhu76b7DkLZkbs5pHy6ZJ7Qhon188KJbzQnsO5gTk40uZwlF2juyseSBiwTkw 0Tugwm+ovGOqB3NVSPZLPLLt4/t0muOPxMtyiuARzQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrjeekgdefudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleffteen ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesph hkshdrihhmpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 1 Aug 2024 06:40:29 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 36972b5f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 1 Aug 2024 10:38:57 +0000 (UTC) Date: Thu, 1 Aug 2024 12:40:26 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Karthik Nayak , Taylor Blau , Junio C Hamano , =?utf-8?b?UnViw6lu?= Justo Subject: [PATCH v2 14/24] builtin/show-branch: fix several memory leaks Message-ID: <20a40e2fd49468f3c406a3124c06cd2b4a9f58eb.1722499961.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: There are several memory leaks in git-show-branch(1). Fix them. Signed-off-by: Patrick Steinhardt --- builtin/show-branch.c | 52 +++++++++++++++++++++++++++++------------- t/t3202-show-branch.sh | 1 + t/t6010-merge-base.sh | 1 + 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/builtin/show-branch.c b/builtin/show-branch.c index d72f4cb98d..7d797a880c 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -502,14 +502,14 @@ static int rev_is_head(const char *head, const char *name) return !strcmp(head, name); } -static int show_merge_base(struct commit_list *seen, int num_rev) +static int show_merge_base(const struct commit_list *seen, int num_rev) { int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); int exit_status = 1; - while (seen) { - struct commit *commit = pop_commit(&seen); + for (const struct commit_list *s = seen; s; s = s->next) { + struct commit *commit = s->item; int flags = commit->object.flags & all_mask; if (!(flags & UNINTERESTING) && ((flags & all_revs) == all_revs)) { @@ -635,7 +635,7 @@ static int parse_reflog_param(const struct option *opt, const char *arg, int cmd_show_branch(int ac, const char **av, const char *prefix) { struct commit *rev[MAX_REVS], *commit; - char *reflog_msg[MAX_REVS]; + char *reflog_msg[MAX_REVS] = {0}; struct commit_list *list = NULL, *seen = NULL; unsigned int rev_mask[MAX_REVS]; int num_rev, i, extra = 0; @@ -692,6 +692,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) parse_reflog_param), OPT_END() }; + const char **args_copy = NULL; + int ret; init_commit_name_slab(&name_slab); @@ -699,8 +701,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) /* If nothing is specified, try the default first */ if (ac == 1 && default_args.nr) { + DUP_ARRAY(args_copy, default_args.v, default_args.nr); ac = default_args.nr; - av = default_args.v; + av = args_copy; } ac = parse_options(ac, av, prefix, builtin_show_branch_options, @@ -780,7 +783,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) } for (i = 0; i < reflog; i++) { - char *logmsg; + char *logmsg = NULL; char *nth_desc; const char *msg; char *end; @@ -790,6 +793,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) if (read_ref_at(get_main_ref_store(the_repository), ref, flags, 0, base + i, &oid, &logmsg, ×tamp, &tz, NULL)) { + free(logmsg); reflog = i; break; } @@ -842,7 +846,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) if (!ref_name_cnt) { fprintf(stderr, "No revs to be shown.\n"); - exit(0); + ret = 0; + goto out; } for (num_rev = 0; ref_name[num_rev]; num_rev++) { @@ -879,11 +884,15 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) commit_list_sort_by_date(&seen); - if (merge_base) - return show_merge_base(seen, num_rev); + if (merge_base) { + ret = show_merge_base(seen, num_rev); + goto out; + } - if (independent) - return show_independent(rev, num_rev, rev_mask); + if (independent) { + ret = show_independent(rev, num_rev, rev_mask); + goto out; + } /* Show list; --more=-1 means list-only */ if (1 < num_rev || extra < 0) { @@ -919,8 +928,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) putchar('\n'); } } - if (extra < 0) - exit(0); + if (extra < 0) { + ret = 0; + goto out; + } /* Sort topologically */ sort_in_topological_order(&seen, sort_order); @@ -932,8 +943,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); all_revs = all_mask & ~((1u << REV_SHIFT) - 1); - while (seen) { - struct commit *commit = pop_commit(&seen); + for (struct commit_list *l = seen; l; l = l->next) { + struct commit *commit = l->item; int this_flag = commit->object.flags; int is_merge_point = ((this_flag & all_revs) == all_revs); @@ -973,6 +984,15 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) if (shown_merge_point && --extra < 0) break; } + + ret = 0; + +out: + for (size_t i = 0; i < ARRAY_SIZE(reflog_msg); i++) + free(reflog_msg[i]); + free_commit_list(seen); + free_commit_list(list); + free(args_copy); free(head); - return 0; + return ret; } diff --git a/t/t3202-show-branch.sh b/t/t3202-show-branch.sh index a1139f79e2..3b6dad0c46 100755 --- a/t/t3202-show-branch.sh +++ b/t/t3202-show-branch.sh @@ -2,6 +2,7 @@ test_description='test show-branch' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'error descriptions on empty repository' ' diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh index 44c726ea39..f96ea82e78 100755 --- a/t/t6010-merge-base.sh +++ b/t/t6010-merge-base.sh @@ -6,6 +6,7 @@ test_description='Merge base and parent list computation. ' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh M=1130000000