🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

fix(refs): attach refdb owner to loose pseudorefs - #7227

Merged
ethomson merged 3 commits into
libgit2:mainfrom
weihanglo:pseudoref
Apr 23, 2026
Merged

fix(refs): attach refdb owner to loose pseudorefs #7227
ethomson merged 3 commits into
libgit2:mainfrom
weihanglo:pseudoref

Conversation

@weihanglo

@weihanglo weihanglo commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

What is the bug

b17ecb2 changed pseudoref lookup to read loose refs directly, bypassing refdb lookup. It introduced a bug that a looked-up FETCH_HEAD pseudoref has no owning refdb attached.

The test in c0a5341 shows that caller may still expect ref->db to have an associated db, hence git_reference_peel failed and gave us a segmentation fault.

This was found this during the integration of Cargo with SHA256 support. In Cargo test suite we have things like this that peels FETCH_HEAD to a commit

repo.find_reference("FETCH_HEAD")?.peel_to_commit()?;

The fix

Attach the repo's refdb after the loose pseudoref is read so
owner-dependent ops such as peeling can safely resolve objects.

Also found a bug in git_reference__lookup_loose that it didn't check allocation error before.

How to reviews

This PR is best reviewed commit by commit, and is bisect-friendly.

  • The first commit documents the problematic behavior, and can be served as minimal reproducer (you can checkout to the commit and confirm the buggy behavior exist).
  • The second commit contains the fix and the test assertion updates. The diff in test shows the behavior change.

b17ecb2 changed pseudoref lookup to read loose refs directly,
bypassing refdb lookup.

This documents the resulting buggy behavior:
a looked-up `FETCH_HEAD` pseudoref has no owning refdb attached.
Pseudoref lookup now reads `FETCH_HEAD` directly as a loose ref,
but callers still expect looked-up references to know their owning
repository.

Attach the repo's refdb after the loose pseudoref is read so
owner-dependent ops such as peeling can safely resolve objects.
The loose ref lookup helper was exposed by 7f35dc5
and then used by b17ecb2 directly for pseudoref lookup.

While investigating the pseudoref owner regression,
I found that the helper also returns success
with a NULL output reference when `git_reference__alloc()`
or `git_reference__alloc_symbolic()` failed.

This fixes it by reporting allocation failures inside
`git_reference__lookup_loose`.
@ethomson

Copy link
Copy Markdown
Member

Good catch - thanks!

@ethomson
ethomson merged commit 9fd0be8 into libgit2:main Apr 23, 2026
19 checks passed
@weihanglo
weihanglo deleted the pseudoref branch April 23, 2026 21:16
@ethomson ethomson added the bug label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants