[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