aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary/varint.go
diff options
context:
space:
mode:
authorEmmanuel T Odeke <emmanuel@orijtech.com>2021-03-06 23:14:21 -0800
committerEmmanuel Odeke <emmanuel@orijtech.com>2021-03-08 18:49:14 +0000
commitaafad20b617ee63d58fcd4f6e0d98fe27760678c (patch)
tree6359968324c31731f8bcf2376759ba48211b3307 /src/encoding/binary/varint.go
parent125eca0f7210da1bbf1a4a1460a87d1c33366b99 (diff)
downloadgo-aafad20b617ee63d58fcd4f6e0d98fe27760678c.tar.xz
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 <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/encoding/binary/varint.go')
-rw-r--r--src/encoding/binary/varint.go7
1 files changed, 6 insertions, 1 deletions
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)<<s, i + 1