aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJavi Fontan <jfontan@gmail.com>2019-02-25 11:42:09 +0100
committerJavi Fontan <jfontan@gmail.com>2019-02-25 15:10:24 +0100
commite9d0393809be173a2b28cb1a60b306468464c7fb (patch)
treeda147d38dde5940994928e3d7096704fff4edc10
parentdb6c41c156481962abf9a55a324858674c25ab08 (diff)
downloadgo-git-e9d0393809be173a2b28cb1a60b306468464c7fb.tar.gz
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>
-rw-r--r--plumbing/cache/object_lru.go5
-rw-r--r--plumbing/cache/object_test.go15
2 files changed, 19 insertions, 1 deletions
diff --git a/plumbing/cache/object_lru.go b/plumbing/cache/object_lru.go
index 53d8b02..cd3712b 100644
--- a/plumbing/cache/object_lru.go
+++ b/plumbing/cache/object_lru.go
@@ -61,6 +61,11 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
c.actualSize += objSize
for c.actualSize > c.MaxSize {
last := c.ll.Back()
+ if last == nil {
+ c.actualSize = 0
+ break
+ }
+
lastObj := last.Value.(plumbing.EncodedObject)
lastSize := FileSize(lastObj.Size())
diff --git a/plumbing/cache/object_test.go b/plumbing/cache/object_test.go
index b3e5f79..2e8fa7b 100644
--- a/plumbing/cache/object_test.go
+++ b/plumbing/cache/object_test.go
@@ -153,6 +153,19 @@ func (s *ObjectSuite) TestDefaultLRU(c *C) {
c.Assert(defaultLRU.MaxSize, Equals, DefaultMaxSize)
}
+func (s *ObjectSuite) TestObjectUpdateOverflow(c *C) {
+ o := NewObjectLRU(9 * Byte)
+
+ a1 := newObject(s.aObject.Hash().String(), 9*Byte)
+ a2 := newObject(s.aObject.Hash().String(), 1*Byte)
+ b := newObject(s.bObject.Hash().String(), 1*Byte)
+
+ o.Put(a1)
+ a1.SetSize(-5)
+ o.Put(a2)
+ o.Put(b)
+}
+
type dummyObject struct {
hash plumbing.Hash
size FileSize
@@ -169,6 +182,6 @@ func (d *dummyObject) Hash() plumbing.Hash { return d.hash }
func (*dummyObject) Type() plumbing.ObjectType { return plumbing.InvalidObject }
func (*dummyObject) SetType(plumbing.ObjectType) {}
func (d *dummyObject) Size() int64 { return int64(d.size) }
-func (*dummyObject) SetSize(s int64) {}
+func (d *dummyObject) SetSize(s int64) { d.size = FileSize(s) }
func (*dummyObject) Reader() (io.ReadCloser, error) { return nil, nil }
func (*dummyObject) Writer() (io.WriteCloser, error) { return nil, nil }