Compare
997c1daf1dce036cd52ba2a4a3d4060b74dd9d54..95d48952cc66b299554ebd90aada5c3fbc2a6e7d
repository: make repo_clear() idempotent
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King <peff@peff.net> · 2026-09-02 05:55 UTC · committed by Junio C Hamano
2 files changed,
+7
−4
modified builtin/worktree.c
| @@ -303,9 +303,11 @@ static const char *worktree_basename(const char *path, int *olen) | |||
| 303 | 303 | while (len && is_dir_sep(path[len - 1])) | |
| 304 | 304 | len--; | |
| 305 | 305 | ||
| 306 | - | name = path + len; | |
| 307 | - | while (name > path && !is_dir_sep(name[-1])) | |
| 308 | - | name--; | |
| 306 | + | for (name = path + len - 1; name > path; name--) | |
| 307 | + | if (is_dir_sep(*name)) { | |
| 308 | + | name++; | |
| 309 | + | break; | |
| 310 | + | } | |
| 309 | 311 | ||
| 310 | 312 | *olen = len; | |
| 311 | 313 | return name; | |
modified repository.c
| @@ -384,7 +384,8 @@ void repo_clear(struct repository *repo) | |||
| 384 | 384 | odb_free(repo->objects); | |
| 385 | 385 | repo->objects = NULL; | |
| 386 | 386 | ||
| 387 | - | parsed_object_pool_clear(repo->parsed_objects); | |
| 387 | + | if (repo->parsed_objects) | |
| 388 | + | parsed_object_pool_clear(repo->parsed_objects); | |
| 388 | 389 | FREE_AND_NULL(repo->parsed_objects); | |
| 389 | 390 | ||
| 390 | 391 | repo_settings_clear(repo); | |