From patchwork Thu Aug 8 13:05:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13757442 Received: from fout7-smtp.messagingengine.com (fout7-smtp.messagingengine.com [103.168.172.150]) (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 D2C6218CC0C for ; Thu, 8 Aug 2024 13:05:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.150 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723122350; cv=none; b=qHCIUKxYPOGFy9bb9xPuIUk8POSV4GTFI9xxafHCdMxZBXEEkBTLBlyb9/m6F4+QCYUsm5Lfvp9NvMKhuclPyQvgRJ3ko6WtYfiUfIAZkw6AzLP6mDmhtTHIIcK2kghI29FRlZEOeGwycFLbMEwsfuEhcdRz+ZkYkED7CM+p2AI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723122350; c=relaxed/simple; bh=AGewfgi60MmoZT02SLkTpw8s5FliFb7W/Oad5/iC6hM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mgVdskALJmlQiFvuPIAwH0jELTKiapY0lBbZtkEdsBV48ql6wEFbM5jrbPVicIqla4wKtAMHMhBtQqK6yFsXVrl5+NUpk8Dflq1yRaEsIACL+kYUFAaatr4lvYq+jBexfizAiC29H0QscpA7mAfrw8oCoh8r3RzA9ClSXVZIotA= 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=JUaz8wWW; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=cW7vr108; arc=none smtp.client-ip=103.168.172.150 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="JUaz8wWW"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="cW7vr108" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfout.nyi.internal (Postfix) with ESMTP id 38F1F138FC7C; Thu, 8 Aug 2024 09:05:48 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Thu, 08 Aug 2024 09:05:48 -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=1723122348; x=1723208748; bh=vAvDZLuY5B O1FtNiA+PmUiAn64uI7BlvdmRdVCfVJEs=; b=JUaz8wWWu+PxDhE50XYSi2I2d2 H624TCwoTkEonVy9bwickVX0/HgEaWfT2TNnj5zodAYNcgYGIj0EoYiQLq6grx3k 2xT5uxtNgxuK5E31dT9GL6pNUpLlfnxdxsLATaekAUxrH5kdAOzinFPw7rBbr0rH j7rPDIK1Vl7d2Mpt5BBFAy+1UWJMIP5U7pqjzL/bRARm79SpD6gzbC9oIBW3amTQ xnp+aTZ2ig6CtKghGeux/zSRTOL+lI9u8fMl87mrer2NQcZFN/6UcZFOWogSZIDs 7r7DPNr+KShCF/7DpjuSez+4pNqz2XY6S23+dP2RfhxWkTs3e6wQKySXeLrg== 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=1723122348; x=1723208748; bh=vAvDZLuY5BO1FtNiA+PmUiAn64uI 7BlvdmRdVCfVJEs=; b=cW7vr108dXmHXRjgXOqB+eWsM571z9A+b4LuqdyK4fl/ t7Xz7x1Fh6iw0LbL8uFMuzQ/UzWUrqVeq9cGmPeM9lGZWGy2cxt3gHLzsaSozwSw rzRbV51FFKljgLkdLie9W8krEd/rueprmPopS0tFc7yY+2x4Zl+K/Rgx28hTNfBo EUuEvIRcFTeCFdL22z/CGp6xwR5ZXi51QWxaMyULvvV2AsWxmKyqb1IE4nYa0cvw aBZSis2jLR7vDDKGXpjk6a7urFfIBZM01U/QzKmpmI4i3PPB+2rYQj/SGdJgs3R8 X6R61cOR+zyoWv5NcWztgwpJkpwPAOPercqRWA8bLQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrledvgdeitdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecu hfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqe enucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeeh gfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedvnecurfgrrhgrmhepmhgrih hlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopeefpdhmohguvgepshhm thhpohhuthdprhgtphhtthhopehjrghmvghssehjrghmvghslhhiuhdrihhopdhrtghpth htohepphhhihhllhhiphdrfihoohguuddvfeesghhmrghilhdrtghomhdprhgtphhtthho pehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 8 Aug 2024 09:05:47 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 234510b5 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 8 Aug 2024 13:05:40 +0000 (UTC) Date: Thu, 8 Aug 2024 15:05:45 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: James Liu , Phillip Wood Subject: [PATCH v2 16/22] unpack-trees: clear index when not propagating it Message-ID: <2755023742e33da6e420638ea1bc90cd31150407.1723121979.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: When provided a pointer to a destination index, then `unpack_trees()` will end up copying its `o->internal.result` index into the provided pointer. In those cases it is thus not necessary to free the index, as we have transferred ownership of it. There are cases though where we do not end up transferring ownership of the memory, but `clear_unpack_trees_porcelain()` will never discard the index in that case and thus cause a memory leak. And right now it cannot do so in the first place because we have no indicator of whether we did or didn't transfer ownership of the index. Adapt the code to zero out the index in case we transfer its ownership. Like this, we can now unconditionally discard the index when being asked to clear the `unpack_trees_options`. Signed-off-by: Patrick Steinhardt --- t/t3705-add-sparse-checkout.sh | 1 + unpack-trees.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/t/t3705-add-sparse-checkout.sh b/t/t3705-add-sparse-checkout.sh index 2bade9e804..6ae45a788d 100755 --- a/t/t3705-add-sparse-checkout.sh +++ b/t/t3705-add-sparse-checkout.sh @@ -2,6 +2,7 @@ test_description='git add in sparse checked out working trees' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh SPARSE_ENTRY_BLOB="" diff --git a/unpack-trees.c b/unpack-trees.c index 7dc884fafd..9a55cb6204 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -210,6 +210,7 @@ void clear_unpack_trees_porcelain(struct unpack_trees_options *opts) { strvec_clear(&opts->internal.msgs_to_free); memset(opts->internal.msgs, 0, sizeof(opts->internal.msgs)); + discard_index(&opts->internal.result); } static int do_add_entry(struct unpack_trees_options *o, struct cache_entry *ce, @@ -2082,6 +2083,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options o->internal.result.updated_workdir = 1; discard_index(o->dst_index); *o->dst_index = o->internal.result; + memset(&o->internal.result, 0, sizeof(o->internal.result)); } else { discard_index(&o->internal.result); }