From 0c9ea959a7e86a1f222a75a23dbc50e3e7863995 Mon Sep 17 00:00:00 2001 From: Tnze Date: Sat, 28 May 2022 14:23:00 +0800 Subject: [PATCH] add unpack packet check to prevent panic --- net/packet/packet.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/packet/packet.go b/net/packet/packet.go index 3bfa370..e929258 100644 --- a/net/packet/packet.go +++ b/net/packet/packet.go @@ -8,6 +8,8 @@ import ( "sync" ) +const MaxDataLength = 2097152 + // Packet define a net data package type Packet struct { ID int32 @@ -170,6 +172,9 @@ func (p *Packet) unpackWithoutCompression(r io.Reader) error { p.ID = int32(PacketID) lengthOfData := int(Length) - int(n) + if lengthOfData < 0 || lengthOfData > MaxDataLength { + return fmt.Errorf("uncompressed packet error: lenght is %d", lengthOfData) + } if cap(p.Data) < lengthOfData { p.Data = make([]byte, lengthOfData) } else { @@ -210,7 +215,6 @@ func (p *Packet) unpackWithCompression(r io.Reader, threshold int) error { if int(DataLength) < threshold { return fmt.Errorf("compressed packet error: size of %d is below threshold of %d", DataLength, threshold) } - const MaxDataLength = 2097152 if DataLength > MaxDataLength { return fmt.Errorf("compressed packet error: size of %d is larger than protocol maximum of %d", DataLength, MaxDataLength) }