buffer.go: Ensure extended part by Extend() & Resize() are all-zero

https://github.com/XTLS/Xray-core/pull/4655#issuecomment-2837693439
This commit is contained in:
RPRX 2025-04-29 07:33:37 +00:00 committed by GitHub
parent 58c48664e2
commit 2eed70e17d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,6 +13,8 @@ const (
Size = 8192 Size = 8192
) )
var zero = [Size * 10]byte{0}
var pool = bytespool.GetPool(Size) var pool = bytespool.GetPool(Size)
// ownership represents the data owner of the buffer. // ownership represents the data owner of the buffer.
@ -150,6 +152,7 @@ func (b *Buffer) Extend(n int32) []byte {
} }
ext := b.v[b.end:end] ext := b.v[b.end:end]
b.end = end b.end = end
copy(ext, zero[:])
return ext return ext
} }
@ -198,6 +201,7 @@ func (b *Buffer) Check() {
// Resize cuts the buffer at the given position. // Resize cuts the buffer at the given position.
func (b *Buffer) Resize(from, to int32) { func (b *Buffer) Resize(from, to int32) {
oldEnd := b.end
if from < 0 { if from < 0 {
from += b.Len() from += b.Len()
} }
@ -210,6 +214,9 @@ func (b *Buffer) Resize(from, to int32) {
b.end = b.start + to b.end = b.start + to
b.start += from b.start += from
b.Check() b.Check()
if b.end > oldEnd {
copy(b.v[oldEnd:b.end], zero[:])
}
} }
// Advance cuts the buffer at the given position. // Advance cuts the buffer at the given position.