From 2eed70e17dda9db003ecf6aa7ca5ce88f76f7a38 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Tue, 29 Apr 2025 07:33:37 +0000 Subject: [PATCH] buffer.go: Ensure extended part by Extend() & Resize() are all-zero https://github.com/XTLS/Xray-core/pull/4655#issuecomment-2837693439 --- common/buf/buffer.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index facf6812..2aa60e6a 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -13,6 +13,8 @@ const ( Size = 8192 ) +var zero = [Size * 10]byte{0} + var pool = bytespool.GetPool(Size) // 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] b.end = end + copy(ext, zero[:]) return ext } @@ -198,6 +201,7 @@ func (b *Buffer) Check() { // Resize cuts the buffer at the given position. func (b *Buffer) Resize(from, to int32) { + oldEnd := b.end if from < 0 { from += b.Len() } @@ -210,6 +214,9 @@ func (b *Buffer) Resize(from, to int32) { b.end = b.start + to b.start += from b.Check() + if b.end > oldEnd { + copy(b.v[oldEnd:b.end], zero[:]) + } } // Advance cuts the buffer at the given position.