Fix Git pack-file parsing issue #5062
Merged
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.
What Does This Do
This change fixes an issue that happens when parsing some Git pack-files.
Currently the logic reads Git object size from the pack file, then reads the number of bytes equal to that size, then decompresses the data that was read.
The problem is that the number that's being read in the first step is the size of the Git object AFTER decompression.
So the second steps reads more bytes than it needs, which is fine in most cases, as decompression stops once the end of the compressed data has been reached.
This is a problem, however, when the Git object is located at the end of the file - in that case the logic tries to read more bytes than there are available, and fails with an error.
The quick fix is to stop reading once the end of the file has been reached.
Motivation
Because of this issue, Git metadata cannot be extracted for some repositories (one example is Mybatis-3, which was used for testing and uncovered this issue).