Skip to content

Commit 2eed70e

Browse files
authored
buffer.go: Ensure extended part by Extend() & Resize() are all-zero
#4655 (comment)
1 parent 58c4866 commit 2eed70e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

common/buf/buffer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
Size = 8192
1414
)
1515

16+
var zero = [Size * 10]byte{0}
17+
1618
var pool = bytespool.GetPool(Size)
1719

1820
// ownership represents the data owner of the buffer.
@@ -150,6 +152,7 @@ func (b *Buffer) Extend(n int32) []byte {
150152
}
151153
ext := b.v[b.end:end]
152154
b.end = end
155+
copy(ext, zero[:])
153156
return ext
154157
}
155158

@@ -198,6 +201,7 @@ func (b *Buffer) Check() {
198201

199202
// Resize cuts the buffer at the given position.
200203
func (b *Buffer) Resize(from, to int32) {
204+
oldEnd := b.end
201205
if from < 0 {
202206
from += b.Len()
203207
}
@@ -210,6 +214,9 @@ func (b *Buffer) Resize(from, to int32) {
210214
b.end = b.start + to
211215
b.start += from
212216
b.Check()
217+
if b.end > oldEnd {
218+
copy(b.v[oldEnd:b.end], zero[:])
219+
}
213220
}
214221

215222
// Advance cuts the buffer at the given position.

0 commit comments

Comments
 (0)