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

In some case,the bytes_queue may lead panic #300

Open
contestjia opened this issue Nov 15, 2021 · 2 comments
Open

In some case,the bytes_queue may lead panic #300

contestjia opened this issue Nov 15, 2021 · 2 comments
Labels

Comments

@contestjia
Copy link
Contributor

What is the issue you are having?


goroutine 1 [running]:
github.com/allegro/bigcache/v3/queue.(*BytesQueue).peek(0xc000107f00, 0x100)
	F:/gotest/pkg/mod/github.com/allegro/bigcache/[email protected]/queue/bytes_queue.go:241 +0x130
github.com/allegro/bigcache/v3/queue.(*BytesQueue).Pop(0xc000107f00)
	F:/gotest/pkg/mod/github.com/allegro/bigcache/[email protected]/queue/bytes_queue.go:162 +0x26
main.main()
	F:/gotest/src/algorithm/switch/switch.go:24 +0x187

Minimal, Complete, and Verifiable Example

package main

import (
	"github.com/allegro/bigcache/v3/queue"
)

func FixSizeByteSlice(n int) []byte {
	b := make([]byte, n)
	for i := range b {
		b[i] = 0xff
	}
	return b
}
func main() {
	queue := queue.NewBytesQueue(2*256+2, 655360, true)
	queue.Push(FixSizeByteSlice(254))
	queue.Push(FixSizeByteSlice(254))
	queue.Pop()
	queue.Push(FixSizeByteSlice(127))
	queue.Push(FixSizeByteSlice(259))
	queue.Pop()
	queue.Pop()
	queue.Pop()
}

Environment:

  • Version (git sha or release):v3.0.1
  • OS (e.g. from /etc/os-release or winver.exe): windows/amd64
  • go version: go1.17
@contestjia contestjia added the bug label Nov 15, 2021
@contestjia
Copy link
Contributor Author

I read the source,the code in allocateAdditionalMemory may lead panic

    if q.tail <= q.head {
			if q.tail != q.head {
				headerEntrySize := getUvarintSize(uint32(q.head - q.tail))
				emptyBlobLen := q.head - q.tail - headerEntrySize
				q.push(make([]byte, emptyBlobLen), emptyBlobLen)
			}

			q.head = leftMarginIndex
			q.tail = q.rightMargin
		}

if q.head - q.tail=128, the headerEntrySize equals to 2, but the emptyBlobLen=126,and in q.push the headerEntrySize=1,not equal to 2

@contestjia
Copy link
Contributor Author

And I use BigCache reproduce the panic:

package main

import (
        "time"

        "github.com/allegro/bigcache/v3"
)

func FixSizeByteSlice(n int) []byte {
        b := make([]byte, n)
        for i := range b {
                b[i] = 0xff
        }
        return b
}
func main() {
        cache, _ := bigcache.NewBigCache(bigcache.Config{
                Shards:       1,
                LifeWindow:   3 * time.Second,
                MaxEntrySize: 52,
        })
        cache.Set("a", FixSizeByteSlice(235))
        time.Sleep(2 * time.Second)
        cache.Set("b", FixSizeByteSlice(235))
        // expire the first element
        time.Sleep(2 * time.Second)
        // insert the before head
        cache.Set("c", FixSizeByteSlice(108))
        // reallocate the memory
        cache.Set("d", FixSizeByteSlice(1024))
        time.Sleep(4 * time.Second)
        cache.Set("e", FixSizeByteSlice(3))
        cache.Set("f", FixSizeByteSlice(3))
        cache.Set("g", FixSizeByteSlice(3))
}
panic: runtime error: slice bounds out of range [:32898] with capacity 3130

goroutine 1 [running]:
github.com/allegro/bigcache/v3/queue.(*BytesQueue).peek(0xc0000c6008, 0x100, 0xaf63da4c8601e926, 0x5b1cc0, 0x62d, 0x0, 0x100000000000000, 0xc0000c6090)
        /root/go/pkg/mod/github.com/allegro/bigcache/[email protected]/queue/bytes_queue.go:241 +0x17c
github.com/allegro/bigcache/v3/queue.(*BytesQueue).Peek(...)
        /root/go/pkg/mod/github.com/allegro/bigcache/[email protected]/queue/bytes_queue.go:186
github.com/allegro/bigcache/v3.(*cacheShard).set(0xc0000c6000, 0x4d5e40, 0x1, 0xaf63da4c8601e926, 0xc000110006, 0x3, 0x3, 0x3, 0xc000110006)
        /root/go/pkg/mod/github.com/allegro/bigcache/[email protected]/shard.go:133 +0xd8
github.com/allegro/bigcache/v3.(*BigCache).Set(0xc00009c820, 0x4d5e40, 0x1, 0xc000110006, 0x3, 0x3, 0x0, 0x0)
        /root/go/pkg/mod/github.com/allegro/bigcache/[email protected]/bigcache.go:133 +0xac
main.main()
        /root/go-test/bigcache/bigcache.go:34 +0x31e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant