Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic: runtime error: index out of range [0] with length 0 #7

Closed
Shinena1998 opened this issue Jan 28, 2022 · 2 comments
Closed

panic: runtime error: index out of range [0] with length 0 #7

Shinena1998 opened this issue Jan 28, 2022 · 2 comments

Comments

@Shinena1998
Copy link

Shinena1998 commented Jan 28, 2022

测试代码:

var c = ecache.NewLRUCache(16, 65535, 100*time.Millisecond)
func BenchmarkName(b *testing.B) {
	for i := 0; i < b.N; i++ {
		c.Put("key", "value")
	}
}

报错如下

goos: darwin
goarch: amd64
pkg: mongoTest
cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
BenchmarkName
panic: runtime error: index out of range [0] with length 0

goroutine 24 [running]:
github.com/orca-zhang/ecache.(*cache).put(0xc000090300, {0x1460fa8, 0x191c108}, 0xc0000a0230, {0x0, 0x0, 0x0}, 0x16ce5d9127de9770, 0x1481be8)
	/pkg/mod/github.com/orca-zhang/ecache@v1.1.0/ecache.go:84 +0x6b6
github.com/orca-zhang/ecache.(*Cache).put(0xc000180050, {0x1460fa8, 0x1}, 0x392c0068000c1ee8, {0x0, 0x0, 0x0})
	/pkg/mod/github.com/orca-zhang/ecache@v1.1.0/ecache.go:165 +0x129
github.com/orca-zhang/ecache.(*Cache).Put(...)
	/pkg/mod/github.com/orca-zhang/ecache@v1.1.0/ecache.go:179
mongoTest.BenchmarkName(0xc000168000)
	/mongoTest/lrucache_test.go:12 +0x9b

看样子只要容量设置为65535,就会触发越界panic

@orca-zhang
Copy link
Owner

感谢反馈,看上去是

return &cache{make([][2]uint16, cap+1), make([]node, cap), make(map[string]uint16, cap), 0}

make里传的cap+1有溢出回转,需要向上转一个精度(比如uint32后+1),我补充测试用例修复一下

@orca-zhang
Copy link
Owner

4dddf1a 添加用例复现
8c25851 已修复

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

No branches or pull requests

2 participants