Compare

1af4c26d6972ebf85633e56e67e28868b43f22be..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

Browse files
2 files changed, +31 −34
modified builtin/checkout.c
@@ -1340,34 +1340,6 @@ enum checkout_command {
1340 1340 CHECKOUT_RESTORE = 3,
1341 1341 };
1342 1342
1343 - static void advise_disambiguating_remotes(enum checkout_command which_command)
1344 - {
1345 - const char *cmdname;
1346 -
1347 - switch (which_command) {
1348 - case CHECKOUT_CHECKOUT:
1349 - cmdname = "checkout";
1350 - break;
1351 - case CHECKOUT_SWITCH:
1352 - cmdname = "switch";
1353 - break;
1354 - default:
1355 - BUG("command <%d> should not reach advise_disambiguating_remotes",
1356 - which_command);
1357 - break;
1358 - }
1359 -
1360 - advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1361 - "you can do so by fully qualifying the name with the --track option:\n"
1362 - "\n"
1363 - " git %s --track origin/<name>\n"
1364 - "\n"
1365 - "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1366 - "one remote, e.g. the 'origin' remote, consider setting\n"
1367 - "checkout.defaultRemote=origin in your config."),
1368 - cmdname);
1369 - }
1370 -
1371 1343 static char *parse_remote_branch(const char *arg,
1372 1344 struct object_id *rev,
1373 1345 int could_be_checkout_paths,
@@ -1383,11 +1355,35 @@ static char *parse_remote_branch(const char *arg,
1383 1355 }
1384 1356
1385 1357 if (!remote && num_matches > 1) {
1386 - if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
1387 - advise_disambiguating_remotes(which_command);
1388 -
1389 - die(_("'%s' matched multiple (%d) remote tracking branches"),
1390 - arg, num_matches);
1358 + if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) {
1359 + const char *cmdname;
1360 +
1361 + switch (which_command) {
1362 + case CHECKOUT_CHECKOUT:
1363 + cmdname = "checkout";
1364 + break;
1365 + case CHECKOUT_SWITCH:
1366 + cmdname = "switch";
1367 + break;
1368 + default:
1369 + BUG("command <%d> should not reach parse_remote_branch",
1370 + which_command);
1371 + break;
1372 + }
1373 +
1374 + advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1375 + "you can do so by fully qualifying the name with the --track option:\n"
1376 + "\n"
1377 + " git %s --track origin/<name>\n"
1378 + "\n"
1379 + "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1380 + "one remote, e.g. the 'origin' remote, consider setting\n"
1381 + "checkout.defaultRemote=origin in your config."),
1382 + cmdname);
1383 + }
1384 +
1385 + die(_("'%s' matched multiple (%d) remote tracking branches"),
1386 + arg, num_matches);
1391 1387 }
1392 1388
1393 1389 return remote;
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);