Skip to content

Commit

Permalink
fix: panic runtime error: index out of range [7] with length 0 (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeans authored Sep 9, 2021
1 parent ce711ba commit c2b72fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,44 @@ func (mc *mockedClock) set(value int64) {
func blob(char byte, len int) []byte {
return bytes.Repeat([]byte{char}, len)
}

//
func TestCache_RepeatedSetWithBiggerEntry(t *testing.T) {

opt := DefaultConfig(time.Second)
opt.Shards = 2 << 10
opt.MaxEntriesInWindow = 1024
opt.MaxEntrySize = 1
opt.HardMaxCacheSize = 1
bc, _ := NewBigCache(opt)

err := bc.Set("2225", make([]byte, 200))
if nil != err {
t.Error(err)
t.FailNow()
}
err = bc.Set("8573", make([]byte, 100))
if nil != err {
t.Error(err)
t.FailNow()
}

err = bc.Set("8573", make([]byte, 450))
if nil != err {
// occur error but go next
t.Logf("%v", err)
}

err = bc.Set("7327", make([]byte, 300))
if nil != err {
t.Error(err)
t.FailNow()
}

err = bc.Set("8573", make([]byte, 200))
if nil != err {
t.Error(err)
t.FailNow()
}

}
2 changes: 2 additions & 0 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (s *cacheShard) set(key string, hashedKey uint64, entry []byte) error {
if previousIndex := s.hashmap[hashedKey]; previousIndex != 0 {
if previousEntry, err := s.entries.Get(int(previousIndex)); err == nil {
resetKeyFromEntry(previousEntry)
//remove hashkey
delete(s.hashmap, hashedKey)
}
}

Expand Down

0 comments on commit c2b72fe

Please sign in to comment.