git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] repository: fix free problem with repo_clear(the_repository)

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 10, 2018, 06:13 UTC
Message-ID
<20180510061310.28265-1-pclouds@gmail.com>
In-Reply-To
<20180509170409.13666-1-pclouds@gmail.com>

the_repository is special. One of the special things about it is that it does not allocate a new index_state object like submodules but points to the global the_index variable instead. As a global variable, the_index cannot be free()'d.

Add an exception for this in repo_clear(). In the future perhaps we would be able to allocate the_repository's index on heap too. Then we can revert this.

the_repository->index remains pointed to a clean the_index even after repo_clear() so that it could still be used next time (e.g. in a crazy use case where a dev switches repo in the same process).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 v2 keeps the_repository->index pointed to the_index even after cleared
 repository.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/repository.c b/repository.c
index a4848c1bd0..4143073f89 100644
--- a/repository.c
+++ b/repository.c
@@ -238,7 +238,8 @@ void repo_clear(struct repository *repo)
 
 	if (repo->index) {
 		discard_index(repo->index);
-		FREE_AND_NULL(repo->index);
+		if (repo->index != &the_index)
+			FREE_AND_NULL(repo->index);
 	}
 }
 
-- 
2.17.0.705.g3525833791
Previous: Stefan Beller
Message 11 of 11 in “repository: fix free problem with repo_clear(the_repository)”
  1. repository: fix free problem with repo_clear(the_repository)Nguyễn Thái Ngọc Duy, May 9, 2018
  2. Brandon WilliamsMay 9, 2018
  3. Elijah NewrenMay 9, 2018
  4. Duy NguyenMay 9, 2018
  5. Stefan BellerMay 9, 2018
  6. Duy NguyenMay 9, 2018
  7. Stefan BellerMay 9, 2018
  8. Duy NguyenMay 10, 2018
  9. Junio C HamanoMay 10, 2018
  10. Stefan BellerMay 10, 2018
  11. repository: fix free problem with repo_clear(the_repository)Nguyễn Thái Ngọc Duy, May 10, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.