allegro / allegro/bigcache

when invoke allocateAdditionalMemory() , if the free space is 128 bytes, bug occurs

未关闭
#253 4 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Go
星标
8.2k
派生
614
平均合并
5 天 12 小时
30 天内合并 PR
1

描述

The `allocateAdditionalMemory` method in `bigcache/queue/bytes_queue.go` file has the following code:
```
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
}
```
When head - tail == 128, the q.push() function actually only stores 127 bytes, one less byte.
A bug occurs when you call the Pop function。
See the following unit test for details:
```
func TestAllocateAdditionalSpaceForInsufficientFreeFragmentedSpaceWhereTailIsBeforeHead(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(200, 0, false)

// when
queue.Push(blob('a', 30)) // header + entry + left margin = 32 bytes
queue.Push(blob('b', 127)) // 32 + 127 + 1 = 160 bytes
queue.Push(blob('c', 20)) // 160 + 20 + 1 = 181
queue.Pop() // space freed at the beginning
queue.Pop() //
queue.Push(blob('d', 30)) // 31 bytes used at the beginning, tail pointer is before head pointer, now free space is 128 bytes
queue.Push(blob('e', 160)) // invoke allocateAdditionalMemory but fill 127 bytes free space (It should be 128 bytes, but 127 are filled, leaving one byte unfilled)

// then
//assertEqual(t, 400, queue.Capacity())
//assertEqual(t, blob('d', 30), pop(queue))
//assertEqual(t, blob(0, 127), pop(queue)) //error
//assertEqual(t, blob('c', 20), pop(queue)) //The data is not expected
//assertEqual(t, blob('e', 160), pop(queue))
}
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。