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

[PATCH 1/2] repository: make repo_clear() idempotent

From
Jeff King <peff@peff.net>
Date
Sep 2, 2026, 05:55 UTC
Message-ID
<20260902055526.GA41747@coredump.intra.peff.net>
In-Reply-To
<20260902055117.GA41587@coredump.intra.peff.net>

Calling repo_clear() twice in a row will segfault because the second call will invoke parse_object_pool_clear() on a NULL pointer. This is not usually a big deal, but we can make some error cleanup a little simpler if callers do not need to worry about invoking it twice.

We can fix it by catching the NULL case. The rest of repo_clear() appears to be idempotent.

Signed-off-by: Jeff King <peff@peff.net>
---
This helps in the next patch, but I think it's also just the path of
least surprise.

Arguably this should not be a pointer at all, but the pool code is weirdly asymmetric. It offers only "new" which allocates a struct, but only "clear" to clean it up (but not deallocate). Might be worth fixing, but out of scope for this series.

 repository.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/repository.c b/repository.c
index db4f9d006e..33ad8984bc 100644
--- a/repository.c
+++ b/repository.c
@@ -385,7 +385,8 @@ void repo_clear(struct repository *repo)
 	odb_free(repo->objects);
 	repo->objects = NULL;
 
-	parsed_object_pool_clear(repo->parsed_objects);
+	if (repo->parsed_objects)
+		parsed_object_pool_clear(repo->parsed_objects);
 	FREE_AND_NULL(repo->parsed_objects);
 
 	repo_settings_clear(repo);
-- 
2.55.0.1067.gf7fc94a55c
Previous: Jeff KingNext: Jeff King
Message 2 of 9 in “fix a leak in submodule error path”
  1. 0/2 fix a leak in submodule error pathJeff King, Sep 2, 2026
  2. 1/2 repository: make repo_clear() idempotentJeff King, Sep 2, 2026
  3. Jeff KingSep 2, 2026
  4. Jeff KingSep 2, 2026
  5. Patrick SteinhardtSep 2, 2026
  6. Junio C HamanoSep 2, 2026
  7. Patrick SteinhardtSep 3, 2026
  8. 2/2 submodule--helper: free URL when repository setup failsJeff King, Sep 2, 2026
  9. Patrick SteinhardtSep 2, 2026

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.