From f50f656a667ccaacf1e2af1b532abc08a361a3af Mon Sep 17 00:00:00 2001 From: Dan Milstein Date: Mon, 29 Aug 2016 11:22:12 -0400 Subject: [PATCH] Fix double-delta unmarshaling to respect actual min header size Turns out its valid to have an overall chunk which is smaller than the full doubleDeltaHeaderBytes size -- if it has a single sample, it doesn't fill the whole header. Updated unmarshalling check to respect this. --- storage/local/delta_test.go | 2 +- storage/local/doubledelta.go | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/storage/local/delta_test.go b/storage/local/delta_test.go index 8ad9985dff..b5d2a2cf34 100644 --- a/storage/local/delta_test.go +++ b/storage/local/delta_test.go @@ -87,7 +87,7 @@ func TestUnmarshalingCorruptedDoubleDeltaReturnsAnError(t *testing.T) { cs[0].marshalToBuf(buf) // Corrupt the length to be every possible too-small value - for i := 0; i < doubleDeltaHeaderBytes; i++ { + for i := 0; i < doubleDeltaHeaderMinBytes; i++ { binary.LittleEndian.PutUint16(buf[doubleDeltaHeaderBufLenOffset:], uint16(i)) diff --git a/storage/local/doubledelta.go b/storage/local/doubledelta.go index 2922600dcd..41a1471d6c 100644 --- a/storage/local/doubledelta.go +++ b/storage/local/doubledelta.go @@ -33,7 +33,8 @@ import ( // - base time delta: 8 bytes // - base value delta: 8 bytes const ( - doubleDeltaHeaderBytes = 37 + doubleDeltaHeaderBytes = 37 + doubleDeltaHeaderMinBytes = 21 // header isn't full for chunk w/ one sample doubleDeltaHeaderBufLenOffset = 0 doubleDeltaHeaderTimeBytesOffset = 2 @@ -250,8 +251,8 @@ func (c *doubleDeltaEncodedChunk) unmarshal(r io.Reader) error { if int(l) > cap(*c) { return fmt.Errorf("chunk length exceeded during unmarshaling: %d", l) } - if int(l) < doubleDeltaHeaderBytes { - return fmt.Errorf("chunk length less than header size: %d < %d", l, doubleDeltaHeaderBytes) + if int(l) < doubleDeltaHeaderMinBytes { + return fmt.Errorf("chunk length less than header size: %d < %d", l, doubleDeltaHeaderMinBytes) } *c = (*c)[:l] @@ -266,8 +267,8 @@ func (c *doubleDeltaEncodedChunk) unmarshalFromBuf(buf []byte) error { if int(l) > cap(*c) { return fmt.Errorf("chunk length exceeded during unmarshaling: %d", l) } - if int(l) < doubleDeltaHeaderBytes { - return fmt.Errorf("chunk length less than header size: %d < %d", l, doubleDeltaHeaderBytes) + if int(l) < doubleDeltaHeaderMinBytes { + return fmt.Errorf("chunk length less than header size: %d < %d", l, doubleDeltaHeaderMinBytes) } *c = (*c)[:l] return nil