diff --git a/go.mod b/go.mod index 1610e5042..e9925980f 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/aws/aws-sdk-go v1.38.60 github.com/cespare/xxhash/v2 v2.1.1 github.com/containerd/containerd v1.5.3 // indirect + github.com/dennwc/varint v1.0.0 github.com/dgryski/go-sip13 v0.0.0-20200911182023-62edffca9245 github.com/digitalocean/godo v1.62.0 github.com/docker/docker v20.10.7+incompatible diff --git a/go.sum b/go.sum index 70d13bd17..fd9bc1d3c 100644 --- a/go.sum +++ b/go.sum @@ -344,6 +344,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/dennwc/varint v1.0.0 h1:kGNFFSSw8ToIy3obO/kKr8U9GZYUAxQEVuix4zfDWzE= +github.com/dennwc/varint v1.0.0/go.mod h1:hnItb35rvZvJrbTALZtY/iQfDs48JKRG1RPpgziApxA= github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0= github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= diff --git a/tsdb/encoding/encoding.go b/tsdb/encoding/encoding.go index 82270ce86..54ab8ff36 100644 --- a/tsdb/encoding/encoding.go +++ b/tsdb/encoding/encoding.go @@ -19,6 +19,7 @@ import ( "hash/crc32" "unsafe" + "github.com/dennwc/varint" "github.com/pkg/errors" ) @@ -139,7 +140,7 @@ func NewDecbufUvarintAt(bs ByteSlice, off int, castagnoliTable *crc32.Table) Dec } b := bs.Range(off, off+binary.MaxVarintLen32) - l, n := binary.Uvarint(b) + l, n := varint.Uvarint(b) if n <= 0 || n > binary.MaxVarintLen32 { return Decbuf{E: errors.Errorf("invalid uvarint %d", n)} } @@ -207,11 +208,17 @@ func (d *Decbuf) Varint64() int64 { if d.E != nil { return 0 } - x, n := binary.Varint(d.B) + // Decode as unsigned first, since that's what the varint library implements. + ux, n := varint.Uvarint(d.B) if n < 1 { d.E = ErrInvalidSize return 0 } + // Now decode "ZigZag encoding" https://developers.google.com/protocol-buffers/docs/encoding#signed_integers. + x := int64(ux >> 1) + if ux&1 != 0 { + x = ^x + } d.B = d.B[n:] return x } @@ -220,7 +227,7 @@ func (d *Decbuf) Uvarint64() uint64 { if d.E != nil { return 0 } - x, n := binary.Uvarint(d.B) + x, n := varint.Uvarint(d.B) if n < 1 { d.E = ErrInvalidSize return 0