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.
This commit is contained in:
Dan Milstein 2016-08-29 11:22:12 -04:00
parent b815956341
commit f50f656a66
2 changed files with 7 additions and 6 deletions

View File

@ -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))

View File

@ -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