Skip to content

patch_parse: do not depend on parsed buffer's lifetime#5158

Merged
ethomson merged 1 commit into
libgit2:masterfrom
pks-t:pks/patch-parsed-lifetime
Jul 20, 2019
Merged

patch_parse: do not depend on parsed buffer's lifetime#5158
ethomson merged 1 commit into
libgit2:masterfrom
pks-t:pks/patch-parsed-lifetime

Conversation

@pks-t

@pks-t pks-t commented Jul 5, 2019

Copy link
Copy Markdown
Member

When parsing a patch from a buffer, we let the patch lines point into
the original buffer. While this is efficient use of resources, this also
ties the lifetime of the parsed patch to the parsed buffer. As this
behaviour is not documented anywhere in our API it is very surprising to
its users.

Untie the lifetime by duplicating the lines into the parsed patch. Add a
test that verifies that lifetimes are indeed independent of each other.


Noticed while testing #5125

When parsing a patch from a buffer, we let the patch lines point into
the original buffer. While this is efficient use of resources, this also
ties the lifetime of the parsed patch to the parsed buffer. As this
behaviour is not documented anywhere in our API it is very surprising to
its users.

Untie the lifetime by duplicating the lines into the parsed patch. Add a
test that verifies that lifetimes are indeed independent of each other.
@ethomson

ethomson commented Jul 5, 2019

Copy link
Copy Markdown
Member

Hmm, so a very quick skim indicates that git_patch_from_buffer is only exposed for tests so zooming out a little bit, does this break every dependency that patches have on other objects? Or are we now copying things out of (say) git_blobs but still dependent on them for other data?

@ethomson

ethomson commented Jul 5, 2019

Copy link
Copy Markdown
Member

Hmm, so a very quick skim ...

Hmm, after a slightly less quick skim, I think that my very quick skim was too quick for me to understand the lifecycle of the buffer and usage. I'm going to have to 👀 this over the weekend to grok it in fullness.

@pks-t

pks-t commented Jul 5, 2019

Copy link
Copy Markdown
Member Author

You can definitely trigger this error by using our public APIs, only. See the "git diff --no-index" example in #5125, which does trigger this error by free'ing the file contents before printing the patch.

@eaigner

eaigner commented Jul 6, 2019

Copy link
Copy Markdown

I think I've just encountered this myself after parsing a patch

BEFORE

diff --git a/test/diff2.txt b/test/diff2.txt
index 212a1d6..0dff799 100644
--- a/test/diff2.txt
+++ b/test/diff2.txt
@@ -1,3 +1,2 @@
 This is just
-a text file to
 changes
\ No newline at end of file

AFTER (contains garbage at 1st diff line)

diff --git a/test/diff2.txt b/test/diff2.txt
index 212a1d6..0dff799 100644
--- a/test/diff2.txt
+++ b/test/diff2.txt
@@ -1,3 +1,2 @@
 n_AT�is just
-a text file to
 changes

@pks-t

pks-t commented Jul 11, 2019

Copy link
Copy Markdown
Member Author

Yeah, definitely looks like the same issue. It's really easy to trigger, unfortunately

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