From patchwork Mon Apr 15 11:42:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13629839 Received: from fout2-smtp.messagingengine.com (fout2-smtp.messagingengine.com [103.168.172.145]) (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 9528B5DF05 for ; Mon, 15 Apr 2024 11:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.145 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713181378; cv=none; b=EJrxHFYIkZrIsMMlcxL2K3EVFOrSZGmRp2uLZRtXY+JMXQ+lfwikar7LfWyckRcFPdvhlttersj6+JOsYW5LDwBD9VsPM036RnIzamsDFH/CCC+4TiobsclhYLFufzEUA9Oy0iqFe3NtxVqA6W4hPEu0VPJ4Iw3VeJI+f1DELy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713181378; c=relaxed/simple; bh=KeD18e/QmaQVaVjMJ9BB1ZTAkzqfuILei6rDqXxT/Sc=; h=Date:From:To:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QQzZVyxw1qdJv9GJMUzt7kT339sXugE6e2my54df0sJzovtpCVrrHhRJ6TzXFr65hOLUk8brvoGFxLQN3rpAi28sOERDA6ClJUs8XONgHwZVg0tWvHtyXBIPK5N6qqOH6hHxfQwTWVzGo8gKPGhd7y3/9LBrA4X4bdasECWjAvo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none 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=IsQHCbJm; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Hzcx9aam; arc=none smtp.client-ip=103.168.172.145 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none 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="IsQHCbJm"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Hzcx9aam" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id A2868138082A for ; Mon, 15 Apr 2024 07:42:55 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 15 Apr 2024 07:42:55 -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=fm2; t=1713181375; x=1713267775; bh=Z8qvWa+aeU zGwDvY62jxMV3DdVu8nyOdJeBCCqgFqiM=; b=IsQHCbJmXfI55ylRnWa07X8WQX EHJu5HwFX/ltV+mzpN33Lox3+xhg7j1TDPR1SW6IC2b+MMDmkMot5bUQqhIYeLLP /o9ohfjUDHa2bQmjb/ouoin38vRpAp03MaDnc886GRKr/mClxWjNGwLHzTjMzyCw lWQIJZ+MGmw0Adve201CsbcL5PVla7f9ZXB5t1LYL9ZYyfyzEOL5TMUobEnKfGRi HRyDLvC+3BhPXcSGtf9ZbvBeCQ5qPb4oaRRK9CdgiUwHDo+jKkBdDDqs7LJRIfrB Hzp8cib9fPMflQoNDYHh0X9LVjSrXCbS/3c75OUjPr0qmHH6Y1CvSd7QlJ2g== 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= fm2; t=1713181375; x=1713267775; bh=Z8qvWa+aeUzGwDvY62jxMV3DdVu8 nyOdJeBCCqgFqiM=; b=Hzcx9aamyZtGnsYd3OfmPa3Tb5Sho4PApwjSd+AAUNHa fmwFpyXgEKSRhEqZr6sNS/kCEXc2GOeWMP3u2/7N5WfQmSVxvdjYWkXC7zgCBmdm Xdk280ej6LN0dcyAJzD9FX3oEWOitqlvgq/WWKwAAzwbv9sAWx7VetHRsLnHQaFo mDcDTyflE1QNjLD+sc7V/PYaIvl35fRlIE4V6s5LqUlWgj4yWcve7wsmEevLbnuZ ewXjlu438CIGOQFwnwskaBO8fgVwkMRcUXdtLg3XVOOk7SxPOyB/TlRCN33jfIbI 1ripD4Gf+CKU29GYTlwbMOMj7/2WcFahQvUkN0nXGA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudejvddggeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghr rghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 15 Apr 2024 07:42:54 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 2bd552c5 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Mon, 15 Apr 2024 11:42:34 +0000 (UTC) Date: Mon, 15 Apr 2024 13:42:52 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 3/5] repository: initialize index in `repo_init()` Message-ID: <96256f9eb30e5aca632942169d5c311b1f245b40.1713180749.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 Git starts, one of the first things it will do is to call `initialize_the_repository()`. This function sets up both the global `the_repository` and `the_index` variables as required. Part of that setup is also to set `the_repository.index = &the_index` so that the index can be accessed via the repository. When calling `repo_init()` on a repository though we set the complete struct to all-zeroes, which will also cause us to unset the `index` pointer. And as we don't re-initialize the index in that function, we will end up with a `NULL` pointer here. This has been fine until now becaues this function is only used to create a new repository. git-init(1) does not access the index at all after initializing the repository, whereas git-checkout(1) only uses `the_index` directly. We are about to remove `the_index` though, which will uncover this partially-initialized repository structure. Refactor the code and create a common `initialize_repository()` function that gets called from `repo_init()` and `initialize_the_repository()`. This function sets up both the repository and the index as required. Like this, we can easily special-case when `repo_init()` gets called with `the_repository`. Signed-off-by: Patrick Steinhardt --- repository.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/repository.c b/repository.c index e15b416944..d64d15d952 100644 --- a/repository.c +++ b/repository.c @@ -25,17 +25,20 @@ static struct repository the_repo; struct repository *the_repository; struct index_state the_index; +static void initialize_repository(struct repository *repo, + struct index_state *index) +{ + repo->index = index; + repo->objects = raw_object_store_new(); + repo->remote_state = remote_state_new(); + repo->parsed_objects = parsed_object_pool_new(); + index_state_init(index, repo); +} + void initialize_the_repository(void) { the_repository = &the_repo; - - the_repo.index = &the_index; - the_repo.objects = raw_object_store_new(); - the_repo.remote_state = remote_state_new(); - the_repo.parsed_objects = parsed_object_pool_new(); - - index_state_init(&the_index, the_repository); - + initialize_repository(the_repository, &the_index); repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); } @@ -188,9 +191,12 @@ int repo_init(struct repository *repo, struct repository_format format = REPOSITORY_FORMAT_INIT; memset(repo, 0, sizeof(*repo)); - repo->objects = raw_object_store_new(); - repo->parsed_objects = parsed_object_pool_new(); - repo->remote_state = remote_state_new(); + if (repo == the_repository) { + initialize_repository(the_repository, &the_index); + } else { + ALLOC_ARRAY(repo->index, 1); + initialize_repository(repo, repo->index); + } if (repo_init_gitdir(repo, gitdir)) goto error;