From cb28dabf623fd3fd41bcc4dd8aa765f17bf2b74f Mon Sep 17 00:00:00 2001 From: Jason Wilder Date: Mon, 5 Oct 2015 12:43:08 -0600 Subject: [PATCH] Make DecodeBlock panic if block size is too small Should never get a block size 9 bytes since Encode always returns the min timestampe and a 1 byte header. If we get this, the engine is confused. --- tsdb/engine/tsm1/encoding.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tsdb/engine/tsm1/encoding.go b/tsdb/engine/tsm1/encoding.go index 3fd6d2b4bb..040a12de66 100644 --- a/tsdb/engine/tsm1/encoding.go +++ b/tsdb/engine/tsm1/encoding.go @@ -21,6 +21,10 @@ const ( // BlockString designates a block encodes string values BlockString = 3 + + // encodedBlockHeaderSize is the size of the header for an encoded block. The first 8 bytes + // are the minimum timestamp of the block. The next byte is a block encoding type indicator. + encodedBlockHeaderSize = 9 ) type Value interface { @@ -120,8 +124,8 @@ func (v Values) DecodeSameTypeBlock(block []byte) Values { // DecodeBlock takes a byte array and will decode into values of the appropriate type // based on the block func DecodeBlock(block []byte) (Values, error) { - if len(block) == 0 { - return Values{}, nil + if len(block) <= encodedBlockHeaderSize { + panic(fmt.Sprintf("decode of short block: got %v, exp %v", len(block), encodedBlockHeaderSize)) } blockType := block[8] @@ -135,10 +139,8 @@ func DecodeBlock(block []byte) (Values, error) { case BlockString: return decodeStringBlock(block) default: + panic(fmt.Sprintf("unknown block type: %d", blockType)) } - - // TODO: add support for other block types - return nil, fmt.Errorf("unknown block type: %d", blockType) } // Deduplicate returns a new Values slice with any values