Skip to content

Fix error where config.Shards = 0 causes "index out of range"#280

Merged
cristaloleg merged 2 commits intoallegro:masterfrom
KDAWS-com:master
Jun 8, 2021
Merged

Fix error where config.Shards = 0 causes "index out of range"#280
cristaloleg merged 2 commits intoallegro:masterfrom
KDAWS-com:master

Conversation

@KarlAustin
Copy link
Contributor

Issue Fixed:

If config.Shards is set to 0 then an "index out of range" error will be generated by bigcache.GetShard() at bigcache.go:220

runtime error: index out of range [6004679098338124500] with length 0
goroutine 98 [running]:
net/http.(*conn).serve.func1(0xc00016c000)
/usr/lib/go-1.13/src/net/http/server.go:1767 +0x139
panic(0xa5e780, 0xc000166160)
/usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
github.com/allegro/bigcache.(*BigCache).getShard(...)
...github.com/allegro/bigcache/bigcache.go:220

func (c *BigCache) getShard(hashedKey uint64) (shard *cacheShard) { return c.shards[hashedKey&c.shardMask] }

As the isPowerofTwo() check considers 0 a power of two.

Test:

TestClosing (bicache_test.go) failed because it didn't specify a number of Shards in its config.

All tests pass with the minor change to TestClosing.

Copy link
Collaborator

@cristaloleg cristaloleg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@cristaloleg
Copy link
Collaborator

@codecov-commenter
Copy link

codecov-commenter commented Jun 8, 2021

Codecov Report

Merging #280 (966cbfd) into master (eb759b5) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #280   +/-   ##
=======================================
  Coverage   86.52%   86.52%           
=======================================
  Files          15       15           
  Lines         638      638           
=======================================
  Hits          552      552           
  Misses         71       71           
  Partials       15       15           
Impacted Files Coverage Δ
utils.go 62.50% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eb759b5...966cbfd. Read the comment docs.

@cristaloleg cristaloleg merged commit ce711ba into allegro:master Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants