Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit b235700

Browse files
authored
Merge pull request #856 from kuba--/fix-840/corrupted-objects
plumbing: packfile, Don't copy empty objects. Fixes #840
2 parents a8a12e0 + 88f0dc3 commit b235700

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

plumbing/format/packfile/delta_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *DeltaSuite) SetUpSuite(c *C) {
6262
target: []piece{{"1", 30}, {"2", 20}, {"7", 40}, {"4", 400},
6363
{"5", 10}},
6464
}, {
65-
description: "A copy operation bigger tan 64kb",
65+
description: "A copy operation bigger than 64kb",
6666
base: []piece{{bigRandStr, 1}, {"1", 200}},
6767
target: []piece{{bigRandStr, 1}},
6868
}}
@@ -72,12 +72,16 @@ var bigRandStr = randStringBytes(100 * 1024)
7272

7373
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
7474

75-
func randStringBytes(n int) string {
75+
func randBytes(n int) []byte {
7676
b := make([]byte, n)
7777
for i := range b {
7878
b[i] = letterBytes[rand.Intn(len(letterBytes))]
7979
}
80-
return string(b)
80+
return b
81+
}
82+
83+
func randStringBytes(n int) string {
84+
return string(randBytes(n))
8185
}
8286

8387
func (s *DeltaSuite) TestAddDelta(c *C) {
@@ -110,3 +114,14 @@ func (s *DeltaSuite) TestIncompleteDelta(c *C) {
110114
c.Assert(err, NotNil)
111115
c.Assert(result, IsNil)
112116
}
117+
118+
func (s *DeltaSuite) TestMaxCopySizeDelta(c *C) {
119+
baseBuf := randBytes(maxCopySize)
120+
targetBuf := baseBuf[0:]
121+
targetBuf = append(targetBuf, byte(1))
122+
123+
delta := DiffDelta(baseBuf, targetBuf)
124+
result, err := PatchDelta(baseBuf, delta)
125+
c.Assert(err, IsNil)
126+
c.Assert(result, DeepEquals, targetBuf)
127+
}

plumbing/format/packfile/diff_delta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func diffDelta(index *deltaIndex, src []byte, tgt []byte) []byte {
111111

112112
rl := l
113113
aOffset := offset
114-
for {
114+
for rl > 0 {
115115
if rl < maxCopySize {
116116
buf.Write(encodeCopyOperation(aOffset, rl))
117117
break

0 commit comments

Comments
 (0)