From patchwork Tue Aug 6 09:00:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13754587 Received: from fout6-smtp.messagingengine.com (fout6-smtp.messagingengine.com [103.168.172.149]) (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 DFACB1BD50E for ; Tue, 6 Aug 2024 09:00:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.149 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722934855; cv=none; b=WxQxUs6eRoan+3FCwOdxgkib2FOoqS+cLP5qwUQgZKNgqBKOp2qkdXdHt+WJpvXGo15TEwZK2YkmbCp5cj50wruszYM+I4wUXAoWQChgRpiZHpk2rv9vdzQQFAYObtL+WfsLfxI51h8VKFn+EBis8Nmy+bo8t8DUvzD3kDAcSXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722934855; c=relaxed/simple; bh=gKLwYZSkELKU+tnijwxsdRSOfKdj7UQTzsM/uLATG7M=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OMISUxIEcwrJkYI3DiIXjedmdPE/g+I4y4DfGGKVEjrOt1JOEjZxyTi5oCfcznQwRcC6NmkhrTJKUXGWHo0knrmM2hZH/s88S79M0x8kAJ1NlNJafif37ToUrc55iCEmoHQhIsPY5FQS3FyWHOj7NGge/gvtHWsLqpNmskT1L7Y= 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=nd+VmA9f; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=U4waNSEI; arc=none smtp.client-ip=103.168.172.149 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="nd+VmA9f"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="U4waNSEI" Received: from compute8.internal (compute8.nyi.internal [10.202.2.227]) by mailfout.nyi.internal (Postfix) with ESMTP id E6E77138FCB9 for ; Tue, 6 Aug 2024 05:00:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute8.internal (MEProxy); Tue, 06 Aug 2024 05:00:52 -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=fm3; t=1722934852; x=1723021252; bh=EtPZGOlke/ IbqPMbOKin9UQ7Xy0Beew9/4awibZAz1E=; b=nd+VmA9fo9jdxP4jPQ4nN+j4Tf Zs3aliZ9G1leLk6v8XYBgcw0GwyfX4N7nr1KYIPC90vSARwo3nDJkjRFJ2Qnz3a8 U8nZPbpgNZNgclqv+XYRtEPSomAMs8AJ7DeklFqGgAOcXHGyd5+unSw3ZKccjy1w TAX+gpCynHj/pS0BJ7ku9MiaCNKdOwTKFknHITUZascjEf4oh1vQJiC/6O6SFghg Qt91TinUd61ceIJq40TsqF70Wa6WVv1+q8nCAXw7Hvbf/uBt8HhFGXGJcAgfsCH6 HvWMy/41cQbW10vsZ9azpoq4Zf0mUbS8eXo02egypGOAXQfZehEunPBZCy6Q== 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= fm3; t=1722934852; x=1723021252; bh=EtPZGOlke/IbqPMbOKin9UQ7Xy0B eew9/4awibZAz1E=; b=U4waNSEIM4+Bxta6E+NWD2zzYhGiZ3Glt811yenhpF72 onKeazAmAUR2KQuZhD28yu8nrt3LRtyilRtSGOWsNpPlkmmYJnVy1lnfwyS25nff SSeQT0RV7DNoudKJW7a58BNgAq9gOVEaSw2ldiZL3ruCbF6BVFKJaztxo2DbGnZd 5sU6SaG246iqneYg5J4U0IKvv6MldTHorlfIF6a0Vom+EAZajc4osVtk3zShGIZR 6vCBQSvM1lQ1+D7D3pNTqvW1zGleCf/J2NsgwfECPvvuAJdcr1WZubG9ZfN2GfEd dHwCovflIhCIG8lor888vVdAXVyWFu03wkVBzoZf3w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrkeekgddtkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffukfhfgggtuggjsehgtderre dttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhk shdrihhmqeenucggtffrrghtthgvrhhnpeehgefhtdefueffheekgfffudelffejtdfhvd ejkedthfehvdelgfetgfdvtedthfenucevlhhushhtvghrufhiiigvpeegnecurfgrrhgr mhepmhgrihhlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopedt X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 6 Aug 2024 05:00:52 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id dd1dcc40 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Tue, 6 Aug 2024 09:00:49 +0000 (UTC) Date: Tue, 6 Aug 2024 11:00:49 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 16/22] unpack-trees: clear index when not propagating it Message-ID: <1f8553fd4343c10a9d2ed2f9bb54937c1cc407c0.1722933642.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); }