From patchwork Thu Aug 8 13:05:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13757436 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 D7EC1EEA9 for ; Thu, 8 Aug 2024 13:05:20 +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=1723122322; cv=none; b=Gdzt/D6FlfUpTOKQfOqUHVnRlGJyDtBQgD+unv5wbyvouJ2SSAfJtXlQRcOfqsid/7qHmsfNfhtYSzOVsilhFzMt8OeIJVrfF3eJ9qH6WFM0t+bI36feZ7LBd2/jo4JuFVjVNP82wXPE6KfhrY5kxUFXZ8uJiABEPuyNsRdi0eQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723122322; c=relaxed/simple; bh=YkNUPnukO+LAwYE3miTCQlN5SqlGB1MUUlG+dOzSSEQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=SM3Z8GpricU+4jE1o8Cil3GOrW8F9Ekrf6ZdGEsPgmIKrgcoCFbIEMt7N22gi/JASOv1in8CATIMAVhNxILt9IcPwKpqL0QGQuCIBtnZY3osb1IA8WQNmfRFy1E/Xr0CYygOC8fTD6Yr1FW/gVRBgGC2jXeKu4zpRFOlQ2dZNP0= 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=oOTILM/E; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=HPisMOM9; 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="oOTILM/E"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="HPisMOM9" Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 4AC6D138FC3A; Thu, 8 Aug 2024 09:05:20 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Thu, 08 Aug 2024 09:05:20 -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=1723122320; x=1723208720; bh=y7T9YAxF68 Rx/lcy5gU0XHzWcJspUY4UkFg+NAkFM6c=; b=oOTILM/EfBAJgPcmAuCP9ctNHO WoHmn85SXtdF4TV839+z3UFUp/9nq/bYMX8CvVFHKOXVMx6nBSnsbu0CLtz+8TtU xGzYAtzbb9+ImvdlMtfHTyiDQWKHtKeUzBfRoTqIEe6kQWfepS3QHHcWLEru+i7A zRLW+L472DIOqBGLaRHOvX1I4VCduw55UMApCkqZ91UFA/7I1ByATPm47MA92mAp xtPxKC0RkX/EkSbnOBLfFN4Mwntv2iK92pABKEPhFYzvjcnViGZFK0QR4S/Flk1+ lWH1R+xi5G6hd4nozlyX3lZKGJEFx1OsA5pK2Ou9HXQAqK+pucjGohI+rEhg== 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=1723122320; x=1723208720; bh=y7T9YAxF68Rx/lcy5gU0XHzWcJsp UY4UkFg+NAkFM6c=; b=HPisMOM9dzK+H2ewgApMqi4XGcSf7BJUV3m+1WcVi3Gv Hpar9ps7gvuA3/OJxGGjW35m02f7DswauFYxqJyAqPJMSCt+EYApqefYMbt+67sP jk0A0GDDefDpHdqwC7M637zjEYZqW2VIGbLdftCGMQUsild6bE5mYEELzj3fiD5k byKd4aVrQg3oPuicoz4ijioDTHjSQtGgd7l4RRF5IwTDdGX2H/txIKLa2fYfc6/4 LYnqkdUcv1wPq2/zj5qq2Yuabsvt6HtdUuYSx5lS0x4TBDcOQOG5GAcEsmBA1nS6 8/OpzpQYMrzfiPrr7Y944sJCN3IEZzhzUg9XPJ0fsA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrledvgdeitdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecu hfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqe enucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeeh gfeltddtheejleffteenucevlhhushhtvghrufhiiigvpeehnecurfgrrhgrmhepmhgrih hlfhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopeefpdhmohguvgepshhm thhpohhuthdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtg hpthhtohepphhhihhllhhiphdrfihoohguuddvfeesghhmrghilhdrtghomhdprhgtphht thhopehjrghmvghssehjrghmvghslhhiuhdrihho X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 8 Aug 2024 09:05:19 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id b62b6b85 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 8 Aug 2024 13:05:11 +0000 (UTC) Date: Thu, 8 Aug 2024 15:05:16 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: James Liu , Phillip Wood Subject: [PATCH v2 10/22] builtin/notes: fix leaking `struct notes_tree` when merging notes 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: We allocate a `struct notes_tree` in `merge_commit()` which we then initialize via `init_notes()`. It's not really necessary to allocate the structure though given that we never pass ownership to the caller. Furthermore, the allocation leads to a memory leak because despite its name, `free_notes()` doesn't free the `notes_tree` but only clears it. Fix this issue by converting the code to use an on-stack variable. Signed-off-by: Patrick Steinhardt --- builtin/notes.c | 9 ++++----- t/t3310-notes-merge-manual-resolve.sh | 1 + t/t3311-notes-merge-fanout.sh | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/builtin/notes.c b/builtin/notes.c index d9c356e354..81cbaeec6b 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -807,7 +807,7 @@ static int merge_commit(struct notes_merge_options *o) { struct strbuf msg = STRBUF_INIT; struct object_id oid, parent_oid; - struct notes_tree *t; + struct notes_tree t = {0}; struct commit *partial; struct pretty_print_context pretty_ctx; void *local_ref_to_free; @@ -830,8 +830,7 @@ static int merge_commit(struct notes_merge_options *o) else oidclr(&parent_oid, the_repository->hash_algo); - CALLOC_ARRAY(t, 1); - init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); + init_notes(&t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); o->local_ref = local_ref_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), @@ -839,7 +838,7 @@ static int merge_commit(struct notes_merge_options *o) if (!o->local_ref) die(_("failed to resolve NOTES_MERGE_REF")); - if (notes_merge_commit(o, t, partial, &oid)) + if (notes_merge_commit(o, &t, partial, &oid)) die(_("failed to finalize notes merge")); /* Reuse existing commit message in reflog message */ @@ -853,7 +852,7 @@ static int merge_commit(struct notes_merge_options *o) is_null_oid(&parent_oid) ? NULL : &parent_oid, 0, UPDATE_REFS_DIE_ON_ERR); - free_notes(t); + free_notes(&t); strbuf_release(&msg); ret = merge_abort(o); free(local_ref_to_free); diff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh index 597df5ebc0..04866b89be 100755 --- a/t/t3310-notes-merge-manual-resolve.sh +++ b/t/t3310-notes-merge-manual-resolve.sh @@ -5,6 +5,7 @@ test_description='Test notes merging with manual conflict resolution' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh # Set up a notes merge scenario with different kinds of conflicts diff --git a/t/t3311-notes-merge-fanout.sh b/t/t3311-notes-merge-fanout.sh index 5b675417e9..ce4144db0f 100755 --- a/t/t3311-notes-merge-fanout.sh +++ b/t/t3311-notes-merge-fanout.sh @@ -5,6 +5,7 @@ test_description='Test notes merging at various fanout levels' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh verify_notes () {