1
0
Эх сурвалжийг харах

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

https://github.com/XTLS/Xray-core/pull/4655#issuecomment-2837693439
RPRX 5 сар өмнө
parent
commit
2eed70e17d
1 өөрчлөгдсөн 7 нэмэгдсэн , 0 устгасан
  1. 7 0
      common/buf/buffer.go

+ 7 - 0
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.