refdb: initialize db in git_refdb_open by cristianchiriac · Pull Request #7430 · libgit2/libgit2 · GitHub
Skip to content

refdb: initialize db in git_refdb_open - #7430

Open
cristianchiriac wants to merge 1 commit into
libgit2:mainfrom
cristianchiriac:refdb-open-oom
Open

cristianchiriac wants to merge 1 commit into
libgit2:mainfrom
cristianchiriac:refdb-open-oom

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #7421.

In git_refdb_open, db was never initialized, and git_refdb_new doesn't set its output when its allocation fails. The error path then called git_refdb_free(db) on an uninitialized pointer. db now starts as NULL, which git_refdb_free ignores. This is the fix suggested in the issue.

Test: refs::oom::lookup_fails_or_finds_head opens testrepo.git and looks up HEAD, which opens the refdb. The lookup runs with an allocator that fails the Nth allocation, for every N until no allocation fails. Each lookup must either report an error or find HEAD -> refs/heads/master. This is the same pattern as blame::oom in #7425 and config::oom in #7429. The repository is opened with the normal allocator, because opening it under the failing one first runs into #7416.

To be upfront about it: on my MinGW build, this test also passes without the fix, because the uninitialized stack slot happens to be zero there. It does run the failing git_refdb_new path, so memory sanitizer builds and builds where the slot holds garbage (the issue reproduces it on Linux at -O0) will catch a regression.

All tests in -srefs pass (MinGW gcc 16, Windows 11).

git_refdb_new does not set its output when its allocation fails, so the
error path in git_refdb_open freed an uninitialized pointer.

Fixes libgit2#7421

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Uninitialized db freed in git_refdb_open on allocation failure

1 participant