From e6ca182ba1030e44159a05f366e3b3274246a869 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Sat, 25 Apr 2020 18:06:37 +0200 Subject: [PATCH] Fixed VarInt and VarLong max size Before VarInt could have had a size of 7 byte without generating errors (max should be 5). >= 5 is because we start from 0 and i=5 is already in error. For example if byte of VarInt are [255 255 255 255 255 255 1], at penultimate iteration, i will be 5 (start at 0) so there aren't errors and at the last the break bypass the sec&0x80 check. Same for VarLong. --- net/packet/types.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/packet/types.go b/net/packet/types.go index c95dda4..cf2a1c7 100644 --- a/net/packet/types.go +++ b/net/packet/types.go @@ -277,10 +277,10 @@ func (v *VarInt) Decode(r DecodeReader) error { n |= uint32(sec&0x7F) << uint32(7*i) - if sec&0x80 == 0 { - break - } else if i > 5 { + if i >= 5 { return errors.New("VarInt is too big") + } else if sec&0x80 == 0 { + break } } @@ -316,10 +316,10 @@ func (v *VarLong) Decode(r DecodeReader) error { n |= uint64(sec&0x7F) << uint64(7*i) - if sec&0x80 == 0 { + if i >= 10 { + return errors.New("VarLong is too big") + } else if sec&0x80 == 0 { break - } else if i > 10 { - return errors.New("VarInt is too big") } }