From patchwork Mon Jun 3 09:48:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13683550 Received: from wfhigh7-smtp.messagingengine.com (wfhigh7-smtp.messagingengine.com [64.147.123.158]) (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 3439D84A34 for ; Mon, 3 Jun 2024 09:48:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717408105; cv=none; b=Ljo9O+kJZGdBBaVThDMkIjzV+nWiGtj9GbrL/Q2BchdLkLQWkCkyTvFuqSGJnIkyjTR4R01dhLB2jhYtNtSkmLuJkYT7qTHpwdgciZteuSHi/GYpWS0RTRJz5UktznEJF3aV9rfJRsJt2eIy9WmH81rFatlOTfWTUAWKLABQVVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717408105; c=relaxed/simple; bh=b8dkX6gBWs1MaoLX7HibSyxT+JvghrKznfJpnY9jmaY=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=gdbJosKJemyzWUy409gLJ0ROOjFfPj4OJ2O2h+D2/p5fzCqUdc7e0ihwHK6VjWFiZGIfMs5yvx+S/N8lil/OReiPUvB2qrDMrshu+Y9DZ6wo8yi5dAlWHWuAmxt2QuyE3+vmhPP15P6QvFVqh7HGXU0n34G2OuAkAW5lzKsJnmc= 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=IleWmJcy; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=HIi7a6Qy; arc=none smtp.client-ip=64.147.123.158 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="IleWmJcy"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="HIi7a6Qy" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfhigh.west.internal (Postfix) with ESMTP id B15F21800099 for ; Mon, 3 Jun 2024 05:48:23 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Mon, 03 Jun 2024 05:48:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=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=fm1; t=1717408103; x=1717494503; bh=CyqMy6x4Ls AnHkHWSYLJtxYZEbM9yqpRsefeEBPt7to=; b=IleWmJcy10gIr37uZ1c2lZbCxy Ha2rheBagYodXFej4oaTL4jXHPlSfjpsftFyOYpjcIo4isag/jtVufuEdSRCrWMp phpO8dL7VTs6TYDQiMEX1uYqhW3M4KdsUNLmoqWIYI2qx4JxdX0LgtImZv0vVwLr hyE2+5i8rr1Lzj66+t30Lqd/ooa9w8BGm87uXr12iaNjm44DwxAK6YXWfqIkNjfU TidGmj0Or7ow0/sj27RICCB3PaVxohy4FxwrCQKbTEre9CtNdfmkt3hpHrXSpuOq cTf6rXabm6puqn6rjo9FBu8mbF8cIIYaOytLrFNf0FEv2HFHUi0MtG8YwjWA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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= fm1; t=1717408103; x=1717494503; bh=CyqMy6x4LsAnHkHWSYLJtxYZEbM9 yqpRsefeEBPt7to=; b=HIi7a6Qy8VojE3zw/oSaw3lNZ+WZ3nDrS/Q08txvuBB7 5t5Tw7G+dPtmwlgwuumHcHEVuI/n81gM/c50Y1Al5Fz1L5L2WJMcIYfTfjXApgCi gxSMFidC+K2wEQ2oYVei703TSxvxyZ6GlfwAU1f8Yiuae1kS4QQc6ToB4s7spI8U iLfAnrT1rnL+EQC0qewRVOTGtPE9zuPs3y5PfADWUT7bgl+Rsct9cQQ7JHDRtDUL 0NISk15LP7mejo+7LK3WTzrt2Lc2O/u7prvqHG6nBNiCvgL0nJrv1pNpLNUM40jY w8jRwY3jUgSWmufFHhfqdYck+J/2/Bpd/MqWtqtzZg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdelvddgvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgepgeenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 3 Jun 2024 05:48:22 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id af89bc07 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Mon, 3 Jun 2024 09:47:58 +0000 (UTC) Date: Mon, 3 Jun 2024 11:48:20 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 24/29] builtin/merge: fix leaking `struct cmdnames` in `get_strategy()` Message-ID: 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: In "builtin/merge.c" we use the helper infrastructure to figure out what merge strategies there are. We never free contents of the `cmdnames` structures though and thus leak their memory. Fix this by exposing the already existing `clean_cmdnames()` function to release their memory. As this name isn't quite idiomatic, rename it to `cmdnames_release()` while at it. Signed-off-by: Patrick Steinhardt --- builtin/merge.c | 10 +++++++--- help.c | 12 ++++++------ help.h | 2 ++ t/t7606-merge-custom.sh | 1 + 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/builtin/merge.c b/builtin/merge.c index 50b0c87a95..682c6ed868 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -164,7 +164,7 @@ static struct strategy *get_strategy(const char *name) { int i; struct strategy *ret; - static struct cmdnames main_cmds, other_cmds; + static struct cmdnames main_cmds = {0}, other_cmds = {0}; static int loaded; char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM"); @@ -182,10 +182,9 @@ static struct strategy *get_strategy(const char *name) return &all_strategy[i]; if (!loaded) { - struct cmdnames not_strategies; + struct cmdnames not_strategies = {0}; loaded = 1; - memset(¬_strategies, 0, sizeof(struct cmdnames)); load_command_list("git-merge-", &main_cmds, &other_cmds); for (i = 0; i < main_cmds.cnt; i++) { int j, found = 0; @@ -197,6 +196,8 @@ static struct strategy *get_strategy(const char *name) add_cmdname(¬_strategies, ent->name, ent->len); } exclude_cmds(&main_cmds, ¬_strategies); + + cmdnames_release(¬_strategies); } if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); @@ -216,6 +217,9 @@ static struct strategy *get_strategy(const char *name) CALLOC_ARRAY(ret, 1); ret->name = xstrdup(name); ret->attr = NO_TRIVIAL; + + cmdnames_release(&main_cmds); + cmdnames_release(&other_cmds); return ret; } diff --git a/help.c b/help.c index 1d057aa607..3686285ca3 100644 --- a/help.c +++ b/help.c @@ -157,7 +157,7 @@ void add_cmdname(struct cmdnames *cmds, const char *name, int len) cmds->names[cmds->cnt++] = ent; } -static void clean_cmdnames(struct cmdnames *cmds) +void cmdnames_release(struct cmdnames *cmds) { int i; for (i = 0; i < cmds->cnt; ++i) @@ -359,8 +359,8 @@ void list_all_main_cmds(struct string_list *list) for (i = 0; i < main_cmds.cnt; i++) string_list_append(list, main_cmds.names[i]->name); - clean_cmdnames(&main_cmds); - clean_cmdnames(&other_cmds); + cmdnames_release(&main_cmds); + cmdnames_release(&other_cmds); } void list_all_other_cmds(struct string_list *list) @@ -375,8 +375,8 @@ void list_all_other_cmds(struct string_list *list) for (i = 0; i < other_cmds.cnt; i++) string_list_append(list, other_cmds.names[i]->name); - clean_cmdnames(&main_cmds); - clean_cmdnames(&other_cmds); + cmdnames_release(&main_cmds); + cmdnames_release(&other_cmds); } void list_cmds_by_category(struct string_list *list, @@ -689,7 +689,7 @@ const char *help_unknown_cmd(const char *cmd) if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { const char *assumed = main_cmds.names[0]->name; main_cmds.names[0] = NULL; - clean_cmdnames(&main_cmds); + cmdnames_release(&main_cmds); fprintf_ln(stderr, _("WARNING: You called a Git command named '%s', " "which does not exist."), diff --git a/help.h b/help.h index af073a7a02..e716ee27ea 100644 --- a/help.h +++ b/help.h @@ -13,6 +13,8 @@ struct cmdnames { } **names; }; +void cmdnames_release(struct cmdnames *cmds); + static inline void mput_char(char c, unsigned int num) { while (num--) diff --git a/t/t7606-merge-custom.sh b/t/t7606-merge-custom.sh index 81fb7c474c..135cb23085 100755 --- a/t/t7606-merge-custom.sh +++ b/t/t7606-merge-custom.sh @@ -14,6 +14,7 @@ Testing a custom strategy. * (tag: c0) c0 " +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'set up custom strategy' '