Repository navigation
Fix the git dir lookup reading past the discovered path - #582
Merged
Merged
Conversation
goneng
force-pushed
the
fix-discovered-git-dir-length
branch
from
August 21, 2026 23:05
608ca10 to
7571fa5
Compare
goneng
marked this pull request as ready for review
August 21, 2026 23:12
git_repository_discover fills a git_buf whose asize is the allocated capacity rather than the length of the path written into it, so the copy took in whatever happened to follow the path in that allocation. - Build the path with initWithBytes:length:encoding: over the buffer's size, dropping the NSData round-trip that existed only to hand a C string to stringWithUTF8String: - Guard on the resulting path rather than on the allocation size, which was non-zero only because libgit2 owns the buffer it hands back - Cover the finder for the first time: a repository root, a nested subdirectory, a directory outside any repository, and a non-file URL
goneng
force-pushed
the
fix-discovered-git-dir-length
branch
from
August 22, 2026 00:30
7571fa5 to
df26a02
Compare
Contributor
nice |
Contributor
This was referenced Sep 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
git_repository_discoverfills agit_bufwhoseasizeis theallocated capacity, not the length of the path it wrote.
gitDirForURL:copied
asizebytes, so theNSDatait built carried the path pluswhatever happened to follow it in that allocation.
initWithBytes:length:encoding:over thebuffer's
size, dropping theNSDataround-trip that existed onlyto hand a C string to
stringWithUTF8String:libgit2 documents
asizeas zero when it does not own the buffer,so that test passed only because
git_repository_discoverhappens toallocate its own
PBRepositoryFinder: repository root, nestedsubdirectory, a directory outside any repository, and a non-file URL
Note that swapping
asizeforsizeon its own would be wrong.sizeexcludes the NUL that libgit2 writes at
ptr[size], so anNSDataofexactly
sizebytes has no terminator andstringWithUTF8String:would scan past the end of it. Hence building the string from the bytes
and a length instead, which does not depend on a terminator at all.
This also unblocks a libgit2 upgrade:
git_buf.asizewas renamedreservedin libgit2 1.x, and this line is the only thing in GitX thatfails to compile against it. With
gitx/objective-git#98checked out,GitX builds and its tests pass against libgit2 1.9.4 once this lands.
Test plan
xcodebuild test -workspace GitX.xcworkspace -scheme GitX -only-testing:GitXTestspasses, 39 tests, up from 35git inita repository in a temporary directoryand resolve symlinks on both sides before comparing, since
NSTemporaryDirectory()is itself a symlinkto
NSUTF16StringEncodingfails both lookup tests while the twonegative tests still pass