From patchwork Thu Apr 18 12:14:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13634604 Received: from fhigh4-smtp.messagingengine.com (fhigh4-smtp.messagingengine.com [103.168.172.155]) (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 1274915FA84 for ; Thu, 18 Apr 2024 12:14:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.155 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713442464; cv=none; b=A14WKHIWFwyZ0TTNtcyWz48iyfnNivkgjWEVYGonZzjxEnEBpBEPphx0Akbr/W8SWwWfNoB7obHEw8NdYOOW8eqIjLhvu38ZTlNo2W/CB39Z7cCcMOVhVc14zSkUZ9J23pMPqbuvzaQon/7wnqSrlr7GVjWZhGIfpR29dcNiAy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713442464; c=relaxed/simple; bh=6HFwFL7ECQrniQUr8ZvO6D2hLf1THGA2FFHYc3I6x24=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VVpgByhKWNERliRFU3I5oupmkVeaU2Y6qmP4wlb4pkEAbFBO2jgKjMqgZdMGumQG5nxvANBu/ldC+e2m4/qiCFfrcAb8oDo8wKZO3odBFxYC6vw6XU31TjFrT0Ob4yB9ozzxYQp5WBEEXEWRj0eoF68mdDpCRAjp7gDpt5GHWnE= 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=d1+52Mlo; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=N6cbN0aI; arc=none smtp.client-ip=103.168.172.155 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="d1+52Mlo"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="N6cbN0aI" Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 2BC2911400E5; Thu, 18 Apr 2024 08:14:22 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 18 Apr 2024 08:14:22 -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=1713442462; x=1713528862; bh=jb0t30xIzF 2bE4pV9COXae/GA8rHO9O/iYCgg15mKeQ=; b=d1+52MlopQPZKjkGQ458lxUOR2 1RfVUxdWt/SoEfc4GQntCpOms923oYLvbvfD/uietH5ygUgD86tzqOlvsfXuDNgY ErFqG4l6qIVl/nOQYgphFU2TPsDmBQjISeRlKARSctpHo3n07RSrTEHAt/hQptjv evVbuDaalo5kQ0L3zG1ndS03045KCH8Z1Xgahf7yl4LnzmpEZSgH6bSvT0rfpfow XyUc9nsMTxTpLv53N3BrBT5c+AE+Wom0VqNMduUmD6ziBUgSPBkBvxaGmAlrqyIK rlTly5Rb1KnRhj/MtZZWwXI5NAGB0SZI/4n0X68GBkqZjhIG62oU9PJZrunQ== 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=1713442462; x=1713528862; bh=jb0t30xIzF2bE4pV9COXae/GA8rH O9O/iYCgg15mKeQ=; b=N6cbN0aI/GdVIJu2QRVl/6bS87I+LSkSV/SUUrWb5mKT N19x2fYLOk3Ic+K5irXH4RS88VrYAY8+f+rVS7WzZ4xvcYPmpIi/1egqvfYl5Abj Ku8KUQFfk16QThg2f8MloPMfplqv9YdI7g7luIAEvZVz6nNHDyUFdH26zx67PkKL h4DaUaf6/blpYBDK9/cDCatXjoNtXiXNtDd6z57WN9wM9ytRDINST/dlI9EgUBFY tc7P3R3rPgE9bJm8NjvrNzgXhVz2xjDwL5JbjBYw/8p0Fd9FoioS0SzhykghlJE+ BR6rBwqfw4ab1UHjabppoNN9V5+2BxQT7ncmns5ytA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudektddggeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Apr 2024 08:14:21 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 15011bc7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 18 Apr 2024 12:13:56 +0000 (UTC) Date: Thu, 18 Apr 2024 14:14:19 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Karthik Nayak , Phillip Wood , Junio C Hamano Subject: [PATCH v2 3/6] repository: initialize index in `repo_init()` Message-ID: <416612e7e31a4b055b3fe17a5cdf78f8d07bd9e2.1713442061.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;