From: Emmanuel T Odeke Date: Sun, 7 Mar 2021 07:14:21 +0000 (-0800) Subject: encoding/binary: limit bytes read by Uvarint to <= 10 X-Git-Tag: go1.17beta1~1238 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=aafad20b617ee63d58fcd4f6e0d98fe27760678c;p=gostls13.git encoding/binary: limit bytes read by Uvarint to <= 10 Limits the number of bytes that can be consumed by Uvarint to MaxVarintLen64 (10) to avoid wasted computations. With this change, if Uvarint reads more than MaxVarintLen64 bytes, it'll return the erroring byte count of n=-(MaxVarintLen64+1) which is -11, as per the function signature. Updated some tests to reflect the new change in expectations of n when the number of bytes to be read exceeds the limits.. Fixes #41185 Change-Id: Ie346457b1ddb0214b60c72e81128e24d604d083d Reviewed-on: https://go-review.googlesource.com/c/go/+/299531 Run-TryBot: Emmanuel Odeke TryBot-Result: Go Bot Reviewed-by: Keith Randall Trust: Emmanuel Odeke --- diff --git a/src/encoding/binary/varint.go b/src/encoding/binary/varint.go index 1fa325dec7..8fe20b5c45 100644 --- a/src/encoding/binary/varint.go +++ b/src/encoding/binary/varint.go @@ -61,8 +61,13 @@ func Uvarint(buf []byte) (uint64, int) { var x uint64 var s uint for i, b := range buf { + if i == MaxVarintLen64 { + // Catch byte reads past MaxVarintLen64. + // See issue https://golang.org/issues/41185 + return 0, -(i + 1) // overflow + } if b < 0x80 { - if i >= MaxVarintLen64 || i == MaxVarintLen64-1 && b > 1 { + if i == MaxVarintLen64-1 && b > 1 { return 0, -(i + 1) // overflow } return x | uint64(b)<