Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
prevent overflowing when delta is minus
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku committed Oct 16, 2021
1 parent 251834f commit c4908fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
24 changes: 14 additions & 10 deletions compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const (
// Link to the paper: https://www.vldb.org/pvldb/vol8/p1816-teller.pdf
type Compressor struct {
bw *bitWriter
header uint32
t uint32
tDelta uint32
header int32
t int32
tDelta int32
leadingZeros uint8
trailingZeros uint8
value uint64
Expand All @@ -26,7 +26,7 @@ type Compressor struct {
// at the end of compressing.
func NewCompressor(w io.Writer, header uint32) (c *Compressor, finish func() error, err error) {
c = &Compressor{
header: header,
header: int32(header),
bw: newBitWriter(w),
leadingZeros: math.MaxUint8,
}
Expand All @@ -40,8 +40,14 @@ func NewCompressor(w io.Writer, header uint32) (c *Compressor, finish func() err
func (c *Compressor) Compress(t uint32, v float64) error {
// First time to compress.
if c.t == 0 {
delta := t - c.header
c.t = t
if int32(t)-c.header < 0 {
// Prevent overflowing of uint64(delta).
// TODO: Consider the better way to handle the case that
// `t` is smaller than `c.header`.
t = uint32(c.header)
}
delta := int32(t) - c.header
c.t = int32(t)
c.tDelta = delta
c.value = math.Float64bits(v)

Expand All @@ -68,11 +74,9 @@ func (c *Compressor) compress(t uint32, v float64) error {
}

func (c *Compressor) compressTimestamp(t uint32) error {
// If t < c.t, delta is overflowed because it is uint32.
// And it causes unexpected EOF during decoding.
delta := t - c.t
delta := int32(t) - c.t
dod := int64(delta) - int64(c.tDelta) // delta of delta
c.t = t
c.t = int32(t)
c.tDelta = delta

// | DoD | Header bits | Value bits | Total bits |
Expand Down
9 changes: 7 additions & 2 deletions gorilla_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ func Test_Compress_Decompress(t *testing.T) {
header := uint32(time.Now().Unix())

const dataLen = 50000
valueFuzz := fuzz.New().NilChance(0)
expected := make([]data, dataLen)
valueFuzz := fuzz.New().NilChance(0)
ts := header
rand.Seed(time.Now().UnixNano())
for i := 0; i < dataLen; i++ {
ts += uint32(rand.Int31n(10000))
if 0 < i && i%10 == 0 {
ts -= uint32(rand.Intn(100))
} else {
ts += uint32(rand.Int31n(100))
}
var v float64
valueFuzz.Fuzz(&v)
expected[i] = data{ts, v}
Expand Down

0 comments on commit c4908fe

Please sign in to comment.