revision: avoid dereferencing NULL in `add_parents_only()`

This function resolves revision suffixes like commit^@ (all parents), commit^! (commit minus parents), and commit^-N (exclude Nth parent). It calls `get_reference()` in a loop to peel through tag objects until it reaches a commit. The existing NULL check after `get_reference()` only handles the ignore_missing case, but get_reference() can return NULL through three distinct paths: 1. revs->ignore_missing: the caller asked to silently skip missing objects. 2. revs->exclude_promisor_objects: the object is a lazy promisor object that should be excluded from the walk. 3. revs->do_not_die_on_missing_objects: the caller wants to record missing OIDs for later reporting (used by `git rev-list --missing=print`) rather than dying. In the latter two instances, the code falls through to dereference the NULL pointer. Handle all three cases explicitly: - ignore_missing: return 0, matching the existing behavior and the pattern in `handle_revision_arg()`. - do_not_die_on_missing_objects: return 0. The missing OID has already been recorded in `revs->missing_commits` by `get_reference()`. Returning 0 is consistent with `handle_revision_arg()` and `process_parents()`, both of which continue without error when this flag is set. The broader codebase pattern for this flag is "record and continue": list-objects.c, builtin/rev-list.c, and process_parents all skip the die/error and keep walking. - everything else (only the `exclude_promisor_objects` case in practice): return -1, consistent with `handle_revision_arg()` where the condition only matches `ignore_missing` or `do_not_die_on_missing_objects`, falling through to ret = -1 for the promisor case. Note: the callers of `add_parents_only()` in `handle_revision_pseudo_opt()` treat any nonzero return as "handled" (`if (add_parents_only(...)) { ret = 0; }`), so the -1 for the promisor case is indistinguishable from success there. This means a promisor-excluded tag target referenced via commit^@ would be silently skipped rather than producing an error. This is a pre-existing limitation of the caller's return value handling and not made worse by this change; the alternative (a NULL dereference crash) _would be_ strictly worse. Pointed out by Coverity. Assisted-by: Claude Opus 4.6 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 10, 2026 at 11:39 UTC f5887e1ec0c89e1cc60dfbfb229a11e72e616cda
2 files changed +25 -2
revision.c
+7 -2
@@ -1904,8 +1904,13 @@ static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1904 return 0;
1905 while (1) {
1906 it = get_reference(revs, arg, &oid, 0);
1907 - if (!it && revs->ignore_missing)
1908 - return 0;
1907 + if (!it) {
1908 + if (revs->ignore_missing)
1909 + return 0;
1910 + if (revs->do_not_die_on_missing_objects)
1911 + return 0;
1912 + return -1;
1913 + }
1914 if (it->type != OBJ_TAG)
1915 break;
1916 if (!((struct tag*)it)->tagged)
t/t0410-partial-clone.sh
+18
@@ -489,6 +489,24 @@ test_expect_success 'rev-list dies for missing objects on cmd line' '
489 done
490 '
491
492 +test_expect_success '--exclude-promisor-objects with ^@ on missing object' '
493 + rm -rf repo &&
494 + test_create_repo repo &&
495 + test_commit -C repo foo &&
496 + test_commit -C repo bar &&
497 +
498 + COMMIT=$(git -C repo rev-parse foo) &&
499 + promise_and_delete "$COMMIT" &&
500 +
501 + git -C repo config core.repositoryformatversion 1 &&
502 + git -C repo config extensions.partialclone "arbitrary string" &&
503 +
504 + # Ensure that "$COMMIT^@" is handled gracefully even though the
505 + # actual commits are missing.
506 + git -C repo rev-list --exclude-promisor-objects "$COMMIT^@" >out &&
507 + test_must_be_empty out
508 +'
509 +
510 test_expect_success 'single promisor remote can be re-initialized gracefully' '
511 # ensure one promisor is in the promisors list
512 rm -rf repo &&