Fix handling of paths with white spaces on diff parsing #6654
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
While I was using pygit2 I got a segmentation fault when trying to use
pygit2.Repository.apply
with a diff obtained by usingparse_diff
. The diff contained a removal of a file with spaces on its path and the path was not quoted:diff --git a/.run/Run IDE with Plugin.run.xml b/.run/Run IDE with Plugin.run.xml
What I found was that when I transformed the diff obtained from
parse_diff
back to a string, the previous line was changed to:diff --git a/.run/Run IDE with Plugin.run.xml /dev/null
For some reason that I still don't fully understand, but that makes some sense to me, the
/dev/null
created a segmentation fault on the apply. I looked into pygit2 and found out that the problem probably was onlibgit2
.Changes
I was able to debug and find out that the function
header_path_len
was the culprit. Since previously this function stopped on a white space if the path is not quoted, then the header I gave as example would be incorrectly parsed. Given this, I changed the function so it only stops when it reachesb/
or"b/
. This still has a problem if a folder calledb
(white space followed by b) or"b
is used but it seems pretty unlikely.Tests
I created a test for delete mode since a test for create mode already existed. However I found it weird that the tests expected that on create mode the result for the old file path was NULL. The reason I found it weird is that for "normal" paths, i.e. paths without spaces, the result was the path of the file instead of NULL. For this reason, I also changed the test for create mode to go accordingly with the behavior of "normal" paths.