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

Commit e9d0393

Browse files
committed
plumbing/cache: check for empty cache list
If there is wrong data in the cache it may cause the eviction code to empty the object list and cause a panic. This patch adds a check and sets the cache usage to 0 when this happens. Signed-off-by: Javi Fontan <jfontan@gmail.com>
1 parent db6c41c commit e9d0393

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

plumbing/cache/object_lru.go

Lines changed: 5 additions & 0 deletions
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

Lines changed: 14 additions & 1 deletion
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)