Skip to content

Commit 0bf2acd

Browse files
authored
fix(buffer): make buffer capacity atleast defaultCapacity (#273)
1 parent 6429872 commit 0bf2acd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

z/buffer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Buffer struct {
5757
}
5858

5959
func NewBuffer(capacity int, tag string) *Buffer {
60-
if capacity == 0 {
60+
if capacity < defaultCapacity {
6161
capacity = defaultCapacity
6262
}
6363
if tag == "" {
@@ -100,7 +100,7 @@ func NewBufferTmp(dir string, capacity int) (*Buffer, error) {
100100
}
101101

102102
func newBufferFile(file *os.File, capacity int) (*Buffer, error) {
103-
if capacity == 0 {
103+
if capacity < defaultCapacity {
104104
capacity = defaultCapacity
105105
}
106106
mmapFile, err := OpenMmapFileUsing(file, capacity, true)

z/buffer_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,18 @@ func newTestBuffers(t *testing.T, capacity int) []*Buffer {
286286

287287
return bufs
288288
}
289+
290+
func TestSmallBuffer(t *testing.T) {
291+
buf := NewBuffer(5, "test")
292+
t.Cleanup(func() {
293+
require.NoError(t, buf.Release())
294+
})
295+
// Write something to buffer so sort actually happens.
296+
buf.WriteSlice([]byte("abc"))
297+
// This test fails if the buffer has offset > currSz.
298+
require.NotPanics(t, func() {
299+
buf.SortSlice(func(left, right []byte) bool {
300+
return true
301+
})
302+
})
303+
}

0 commit comments

Comments
 (0)