Skip to content

Commit 817414b

Browse files
authored
Merge pull request #328 from arangodb-helper/bugfix/limited-buffer-write
Fix LimitedBuffer Write(): out of index error
2 parents 3aad7d8 + 54e779e commit 817414b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pkg/utils/buffer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func (b *LimitedBuffer) Write(p []byte) (n int, err error) {
5757
if newLength <= b.limit {
5858
b.buf = append(b.buf, p...)
5959
} else {
60-
truncateIndex := newLength - b.limit - 1
61-
b.buf = append(b.buf[truncateIndex-1:], p...)
60+
truncateIndex := newLength - b.limit
61+
b.buf = append(b.buf[truncateIndex:], p...)
6262
}
6363
}
6464
return gotLen, nil

pkg/utils/buffer_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ func TestCaptureWriter(t *testing.T) {
6262
assert.Equal(t, "1234567890", cw.String())
6363
})
6464

65+
t.Run("one-byte-overflow-1", func(t *testing.T) {
66+
cw := NewLimitedBuffer(10)
67+
68+
_, _ = cw.Write([]byte(strings.Repeat("001234", 1)))
69+
_, _ = cw.Write([]byte(strings.Repeat("56789", 1)))
70+
assert.Equal(t, strings.Repeat("0123456789", 1), cw.String())
71+
})
72+
73+
t.Run("one-byte-overflow-2", func(t *testing.T) {
74+
cw := NewLimitedBuffer(10)
75+
76+
_, _ = cw.Write([]byte(strings.Repeat("00123", 1)))
77+
_, _ = cw.Write([]byte(strings.Repeat("456789", 1)))
78+
assert.Equal(t, strings.Repeat("0123456789", 1), cw.String())
79+
})
80+
6581
t.Run("overflow-many", func(t *testing.T) {
6682
cw := NewLimitedBuffer(100)
6783

0 commit comments

Comments
 (0)