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

Commit 0106dab

Browse files
authored
Merge pull request #1076 from jfontan/panic-cache
plumbing/cache: check for empty cache list
2 parents db6c41c + e9d0393 commit 0106dab

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

plumbing/cache/object_lru.go

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
6161
c.actualSize += objSize
6262
for c.actualSize > c.MaxSize {
6363
last := c.ll.Back()
64+
if last == nil {
65+
c.actualSize = 0
66+
break
67+
}
68+
6469
lastObj := last.Value.(plumbing.EncodedObject)
6570
lastSize := FileSize(lastObj.Size())
6671

plumbing/cache/object_test.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ func (s *ObjectSuite) TestDefaultLRU(c *C) {
153153
c.Assert(defaultLRU.MaxSize, Equals, DefaultMaxSize)
154154
}
155155

156+
func (s *ObjectSuite) TestObjectUpdateOverflow(c *C) {
157+
o := NewObjectLRU(9 * Byte)
158+
159+
a1 := newObject(s.aObject.Hash().String(), 9*Byte)
160+
a2 := newObject(s.aObject.Hash().String(), 1*Byte)
161+
b := newObject(s.bObject.Hash().String(), 1*Byte)
162+
163+
o.Put(a1)
164+
a1.SetSize(-5)
165+
o.Put(a2)
166+
o.Put(b)
167+
}
168+
156169
type dummyObject struct {
157170
hash plumbing.Hash
158171
size FileSize
@@ -169,6 +182,6 @@ func (d *dummyObject) Hash() plumbing.Hash { return d.hash }
169182
func (*dummyObject) Type() plumbing.ObjectType { return plumbing.InvalidObject }
170183
func (*dummyObject) SetType(plumbing.ObjectType) {}
171184
func (d *dummyObject) Size() int64 { return int64(d.size) }
172-
func (*dummyObject) SetSize(s int64) {}
185+
func (d *dummyObject) SetSize(s int64) { d.size = FileSize(s) }
173186
func (*dummyObject) Reader() (io.ReadCloser, error) { return nil, nil }
174187
func (*dummyObject) Writer() (io.WriteCloser, error) { return nil, nil }

0 commit comments

Comments
 (0)