New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plumbing: format/packfile, prevent large objects from being read into memory completely #330
Merged
mcuadros
merged 6 commits into
go-git:master
from
zeripath:fix-323-fix-broken-large-ofs-delta
Jun 30, 2021
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa47367
Prevent large objects from being read into memory completely (take 2)
zeripath 22550b6
add tests
zeripath a1fc3fc
prod CI
zeripath 1d722fd
Don't add to the object cache
zeripath fb6894b
renudge CI
zeripath 85feea7
Make the LargeObjectThreshold optional
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package packfile | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"io" | ||
"math" | ||
|
||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/go-git/go-git/v5/utils/ioutil" | ||
|
@@ -73,6 +75,131 @@ func PatchDelta(src, delta []byte) ([]byte, error) { | |
return b.Bytes(), nil | ||
} | ||
|
||
func ReaderFromDelta(h *ObjectHeader, base plumbing.EncodedObject, deltaRC io.Reader) (io.ReadCloser, error) { | ||
deltaBuf := bufio.NewReaderSize(deltaRC, 1024) | ||
srcSz, err := decodeLEB128ByteReader(deltaBuf) | ||
if err != nil { | ||
if err == io.EOF { | ||
return nil, ErrInvalidDelta | ||
} | ||
return nil, err | ||
} | ||
if srcSz != uint(base.Size()) { | ||
return nil, ErrInvalidDelta | ||
} | ||
|
||
targetSz, err := decodeLEB128ByteReader(deltaBuf) | ||
if err != nil { | ||
if err == io.EOF { | ||
return nil, ErrInvalidDelta | ||
} | ||
return nil, err | ||
} | ||
remainingTargetSz := targetSz | ||
|
||
dstRd, dstWr := io.Pipe() | ||
|
||
go func() { | ||
baseRd, err := base.Reader() | ||
if err != nil { | ||
_ = dstWr.CloseWithError(ErrInvalidDelta) | ||
return | ||
} | ||
defer baseRd.Close() | ||
|
||
baseBuf := bufio.NewReader(baseRd) | ||
basePos := uint(0) | ||
|
||
for { | ||
cmd, err := deltaBuf.ReadByte() | ||
if err == io.EOF { | ||
_ = dstWr.CloseWithError(ErrInvalidDelta) | ||
return | ||
} | ||
if err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
|
||
if isCopyFromSrc(cmd) { | ||
offset, err := decodeOffsetByteReader(cmd, deltaBuf) | ||
if err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
sz, err := decodeSizeByteReader(cmd, deltaBuf) | ||
if err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
|
||
if invalidSize(sz, targetSz) || | ||
invalidOffsetSize(offset, sz, srcSz) { | ||
_ = dstWr.Close() | ||
return | ||
} | ||
|
||
discard := offset - basePos | ||
if basePos > offset { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line was changed from the original PR where it was
This was the cause of the truncated checkouts. |
||
_ = baseRd.Close() | ||
baseRd, err = base.Reader() | ||
if err != nil { | ||
_ = dstWr.CloseWithError(ErrInvalidDelta) | ||
return | ||
} | ||
baseBuf.Reset(baseRd) | ||
discard = offset | ||
} | ||
for discard > math.MaxInt32 { | ||
n, err := baseBuf.Discard(math.MaxInt32) | ||
if err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
basePos += uint(n) | ||
discard -= uint(n) | ||
} | ||
for discard > 0 { | ||
n, err := baseBuf.Discard(int(discard)) | ||
if err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
basePos += uint(n) | ||
discard -= uint(n) | ||
} | ||
if _, err := io.Copy(dstWr, io.LimitReader(baseBuf, int64(sz))); err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
remainingTargetSz -= sz | ||
basePos += sz | ||
} else if isCopyFromDelta(cmd) { | ||
sz := uint(cmd) // cmd is the size itself | ||
if invalidSize(sz, targetSz) { | ||
_ = dstWr.CloseWithError(ErrInvalidDelta) | ||
return | ||
} | ||
if _, err := io.Copy(dstWr, io.LimitReader(deltaBuf, int64(sz))); err != nil { | ||
_ = dstWr.CloseWithError(err) | ||
return | ||
} | ||
|
||
remainingTargetSz -= sz | ||
} else { | ||
_ = dstWr.CloseWithError(ErrDeltaCmd) | ||
return | ||
} | ||
if remainingTargetSz <= 0 { | ||
_ = dstWr.Close() | ||
return | ||
} | ||
} | ||
}() | ||
|
||
return dstRd, nil | ||
} | ||
|
||
func patchDelta(dst *bytes.Buffer, src, delta []byte) error { | ||
if len(delta) < deltaSizeMin { | ||
return ErrInvalidDelta | ||
|
@@ -161,6 +288,25 @@ func decodeLEB128(input []byte) (uint, []byte) { | |
return num, input[sz:] | ||
} | ||
|
||
func decodeLEB128ByteReader(input io.ByteReader) (uint, error) { | ||
var num, sz uint | ||
for { | ||
b, err := input.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
num |= (uint(b) & payload) << (sz * 7) // concats 7 bits chunks | ||
sz++ | ||
|
||
if uint(b)&continuation == 0 { | ||
break | ||
} | ||
} | ||
|
||
return num, nil | ||
} | ||
|
||
const ( | ||
payload = 0x7f // 0111 1111 | ||
continuation = 0x80 // 1000 0000 | ||
|
@@ -174,6 +320,40 @@ func isCopyFromDelta(cmd byte) bool { | |
return (cmd&0x80) == 0 && cmd != 0 | ||
} | ||
|
||
func decodeOffsetByteReader(cmd byte, delta io.ByteReader) (uint, error) { | ||
var offset uint | ||
if (cmd & 0x01) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
offset = uint(next) | ||
} | ||
if (cmd & 0x02) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
offset |= uint(next) << 8 | ||
} | ||
if (cmd & 0x04) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
offset |= uint(next) << 16 | ||
} | ||
if (cmd & 0x08) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
offset |= uint(next) << 24 | ||
} | ||
|
||
return offset, nil | ||
} | ||
|
||
func decodeOffset(cmd byte, delta []byte) (uint, []byte, error) { | ||
var offset uint | ||
if (cmd & 0x01) != 0 { | ||
|
@@ -208,6 +388,36 @@ func decodeOffset(cmd byte, delta []byte) (uint, []byte, error) { | |
return offset, delta, nil | ||
} | ||
|
||
func decodeSizeByteReader(cmd byte, delta io.ByteReader) (uint, error) { | ||
var sz uint | ||
if (cmd & 0x10) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
sz = uint(next) | ||
} | ||
if (cmd & 0x20) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
sz |= uint(next) << 8 | ||
} | ||
if (cmd & 0x40) != 0 { | ||
next, err := delta.ReadByte() | ||
if err != nil { | ||
return 0, err | ||
} | ||
sz |= uint(next) << 16 | ||
} | ||
if sz == 0 { | ||
sz = 0x10000 | ||
} | ||
|
||
return sz, nil | ||
} | ||
|
||
func decodeSize(cmd byte, delta []byte) (uint, []byte, error) { | ||
var sz uint | ||
if (cmd & 0x10) != 0 { | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR differs from the previous #303 here by wrapping the reader in a reader at - so as to prevent problems from the scanner moving on etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was also a bug here in the original #303 that this fixes.