Skip to content

Fix the git dir lookup reading past the discovered path - #582

Merged
hannesa2 merged 1 commit into
gitx:masterfrom
goneng:fix-discovered-git-dir-length
Aug 22, 2026
Merged

hannesa2 merged 1 commit into
gitx:masterfrom
goneng:fix-discovered-git-dir-length

Conversation

@goneng

@goneng goneng commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

git_repository_discover fills a git_buf whose asize is the
allocated capacity, not the length of the path it wrote. gitDirForURL:
copied asize bytes, so the NSData it built carried the path plus
whatever happened to follow it 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.
    libgit2 documents asize as zero when it does not own the buffer,
    so that test passed only because git_repository_discover happens to
    allocate its own
  • Add the first tests for PBRepositoryFinder: repository root, nested
    subdirectory, a directory outside any repository, and a non-file URL

Note that swapping asize for size on its own would be wrong. size
excludes the NUL that libgit2 writes at ptr[size], so an NSData of
exactly size bytes has no terminator and stringWithUTF8String:
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.asize was renamed
reserved in libgit2 1.x, and this line is the only thing in GitX that
fails to compile against it. With gitx/objective-git#98 checked 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:GitXTests passes, 39 tests, up from 35
  • The four new tests git init a repository in a temporary directory
    and resolve symlinks on both sides before comparing, since
    NSTemporaryDirectory() is itself a symlink
  • Mutation-checked that the new tests have teeth: changing the encoding
    to NSUTF16StringEncoding fails both lookup tests while the two
    negative tests still pass

@goneng goneng changed the title Take the discovered git dir from the buffer's used length Fix the git dir lookup reading past the discovered path Aug 21, 2026
@goneng
goneng force-pushed the fix-discovered-git-dir-length branch from 608ca10 to 7571fa5 Compare August 21, 2026 23:05
@goneng
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
goneng force-pushed the fix-discovered-git-dir-length branch from 7571fa5 to df26a02 Compare August 22, 2026 00:30
@hannesa2

Copy link
Copy Markdown
Contributor

This also unblocks a libgit2 upgrade

nice

@hannesa2
hannesa2 merged commit b914260 into gitx:master Aug 22, 2026
3 checks passed
@goneng
goneng deleted the fix-discovered-git-dir-length branch August 22, 2026 06:51
@hannesa2

Copy link
Copy Markdown
Contributor

Looks like this causes #606
This artifact #608 comes with that issue
@goneng

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