Skip to content

blame: report a failure in git_blame_buffer - #7425

Open
cristianchiriac wants to merge 2 commits into
libgit2:mainfrom
cristianchiriac:blame-buffer-report-diff-error
Open

cristianchiriac wants to merge 2 commits into
libgit2:mainfrom
cristianchiriac:blame-buffer-report-diff-error

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #7418. Builds on #7424, which is the first commit here. Without it, xdiff's own failures still look like success to the blame. I'll rebase once #7424 is merged, or I can fold both into one PR if you prefer.

git_blame_buffer ignored the result of git_diff_blob_to_buffer and returned 0, so a failed diff gave the caller a blame whose hunks didn't reflect the buffer. Making all of an allocation-failure sweep pass turned up three more places that drop a failure the same way:

  • git_blame_buffer: return the diff's error and free the partial blame. When duplicating a reference hunk or inserting it fails, also free the blame, which leaked before.
  • buffer_hunk_cb / buffer_line_cb: the result of git_vector_insert / git_vector_insert_sorted was ignored, so a hunk could be dropped silently. They now free the hunk and return -1, which aborts the diff with the error.
  • split_hunk_in_vector: the original hunk was shortened before an insert whose result was ignored, losing its lines when the insert failed. It now inserts first and returns NULL on failure, which the only caller (buffer_hunk_cb) already reports.

Test: blame::oom::buffer_fails_or_has_every_hunk blames a buffer with one changed line against blametest.git/b.txt. It does this with an allocator that fails the Nth allocation, for every N, until no allocation fails. Each run must either fail or return the same number of hunks as the blame made without failures.

-sblame -sdiff -smerge -sapply -spatch: 617 tests pass (MinGW gcc 16, Windows 11).

git_xdiff ignored the return value of xdl_diff and returned only the
error recorded by the hunk and line callbacks. When xdiff fails, for
example out of memory while preparing the diff, it does so before any
callback runs, so git_patch_from_buffers and the other text diff users
reported success with no hunks.

Fixes libgit2#7420
git_blame_buffer ignored the result of git_diff_blob_to_buffer and
returned success unconditionally, so a failed diff gave a blame whose
hunks did not reflect the buffer. Return the error and free the partial
blame. The hunk callbacks and split_hunk_in_vector also ignored a failed
vector insert, which dropped a hunk silently; report those failures too,
and free the blame when duplicating the reference hunks fails.

Fixes libgit2#7418

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] git_blame_buffer ignores diff errors and returns wrong hunks

1 participant