Skip to content

diff_file: properly refcount blobs when initializing file contents#4447

Merged
ethomson merged 1 commit into
libgit2:masterfrom
pks-t:pks/diff-file-contents-refcount-blob
Dec 16, 2017
Merged

diff_file: properly refcount blobs when initializing file contents#4447
ethomson merged 1 commit into
libgit2:masterfrom
pks-t:pks/diff-file-contents-refcount-blob

Conversation

@pks-t
Copy link
Copy Markdown
Member

@pks-t pks-t commented Dec 15, 2017

When initializing a git_diff_file_content from a source whose data is
derived from a blob, we simply assign the blob's pointer to the
resulting struct without incrementing its refcount. Thus, the structure
can only be used as long as the blob is kept alive by the caller.

Fix the issue by using git_blob_dup instead of a direct assignment.
This function will increment the refcount of the blob without allocating
new memory, so it does exactly what we want. As
git_diff_file_content__unload already frees the blob when
GIT_DIFF_FLAG__FREE_BLOB is set, we don't need to add new code
handling the free but only have to set that flag correctly.

When initializing a `git_diff_file_content` from a source whose data is
derived from a blob, we simply assign the blob's pointer to the
resulting struct without incrementing its refcount. Thus, the structure
can only be used as long as the blob is kept alive by the caller.

Fix the issue by using `git_blob_dup` instead of a direct assignment.
This function will increment the refcount of the blob without allocating
new memory, so it does exactly what we want. As
`git_diff_file_content__unload` already frees the blob when
`GIT_DIFF_FLAG__FREE_BLOB` is set, we don't need to add new code
handling the free but only have to set that flag correctly.
@pks-t
Copy link
Copy Markdown
Member Author

pks-t commented Dec 15, 2017

This should probably fix #4442. The test case I added didn't trigger any faults locally at my machine, so I'm not certain that it actually tests what I'd like it to test.

@brandonio21
Copy link
Copy Markdown

brandonio21 commented Dec 15, 2017

Thanks for the quick fix! The changes and included test LGTM! I'll later test to ensure that this fixes the problem

@brandonio21
Copy link
Copy Markdown

Just tested it on my machine!

Without the fix:

  1) Failure:
diff::blob::patch_with_freed_blobs [/home/bmilton/libgit2-0.26.0/tests/diff/blob.c:101]
  String mismatch: buf.ptr != BLOB_DIFF
  'diff --git a/file b/file
index 45141a7..4d713dc 100644
--- a/file
+++ b/file
@@ -1 +1,6 @@
 p�' != 'diff --git a/file b/file
index 45141a7..4d713dc 100644
--- a/file
+++ b/file
@@ -1 +1,6 @@
 Hello from the root
+
+Some additional lines
+
+Down here below
+
' (at byte 92)

With the fix, all tests pass. It looks good to me! Let's merge this in!

@ethomson
Copy link
Copy Markdown
Member

Looks good to me. 😁

Thanks @brandonio21 for debugging and the helpful bug report; thanks @pks-t for the fix.

@ethomson ethomson merged commit fa8cf14 into libgit2:master Dec 16, 2017
@pks-t pks-t added the backport label Jan 11, 2018
@pks-t pks-t mentioned this pull request Jan 12, 2018
@pks-t pks-t deleted the pks/diff-file-contents-refcount-blob branch July 11, 2019 19:05
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.

3 participants