Repository: allegro/bigcache
Pull request #288 (2021-09-07 12:18:37)

fix: panic runtime error: index out of range [7] with length 0
issue : https://github.com/allegro/bigcache/issues/283

janisz: @cristaloleg @bgalek Can you take a look at this?
codecov-commenter: # [Codecov](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#288](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (cbb971e) into [master](https://codecov.io/gh/allegro/bigcache/commit/eb759b55c367f721a70683400384cede3cb97edf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (eb759b5) will **increase** coverage by `2.47%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/allegro/bigcache/pull/288/graphs/tree.svg?width=650&height=150&src=pr&token=NRx0gXD6he&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

```diff
@@            Coverage Diff             @@
##           master     #288      +/-   ##
==========================================
+ Coverage   86.52%   88.99%   +2.47%     
==========================================
  Files          15       15              
  Lines         638      763     +125     
==========================================
+ Hits          552      679     +127     
+ Misses         71       70       -1     
+ Partials       15       14       -1     
```


| [Impacted Files](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [shard.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c2hhcmQuZ28=) | `88.49% <100.00%> (+3.56%)` | :arrow_up: |
| [utils.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-dXRpbHMuZ28=) | `66.66% <100.00%> (+4.16%)` | :arrow_up: |
| [fnv.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Zm52Lmdv) | `100.00% <0.00%> (ø)` | |
| [bytes.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Ynl0ZXMuZ28=) | `100.00% <0.00%> (ø)` | |
| [clock.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y2xvY2suZ28=) | `100.00% <0.00%> (ø)` | |
| [config.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29uZmlnLmdv) | `100.00% <0.00%> (ø)` | |
| [logger.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-bG9nZ2VyLmdv) | `100.00% <0.00%> (ø)` | |
| [encoding.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-ZW5jb2RpbmcuZ28=) | `100.00% <0.00%> (ø)` | |
| [server/middleware.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c2VydmVyL21pZGRsZXdhcmUuZ28=) | `100.00% <0.00%> (ø)` | |
| [bigcache.go](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-YmlnY2FjaGUuZ28=) | `99.10% <0.00%> (+0.31%)` | :arrow_up: |
| ... and [7 more](https://codecov.io/gh/allegro/bigcache/pull/288/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [ce711ba...cbb971e](https://codecov.io/gh/allegro/bigcache/pull/288?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).

janisz: @blackbeans Looks like tests are failing
janisz: This PR fixes:

- #283
- #254
- #250
- #234
- #226
- #222
blackbeans: > @blackbeans Looks like tests are failing

i resubmit . TestCase should use t.Logf to print error ,not use t.Error
janisz: @cristaloleg PTAL
blackbeans: Please merge this pull request earlier.
janisz: @cristaloleg can you merge it? I have no permission to do that's :(

Patch:
diff --git a/shard.go b/shard.go
index cd352a1..37b0952 100644
--- a/shard.go
+++ b/shard.go
@@ -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)
 		}
 	}
 


Test patch:
diff --git a/bigcache_test.go b/bigcache_test.go
index 2840167..b62a5a9 100644
--- a/bigcache_test.go
+++ b/bigcache_test.go
@@ -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()
+	}
+
+}