diff --git a/tsdb/chunks/head_chunks.go b/tsdb/chunks/head_chunks.go index 41fce69c72..5e143b8b32 100644 --- a/tsdb/chunks/head_chunks.go +++ b/tsdb/chunks/head_chunks.go @@ -20,6 +20,7 @@ import ( "fmt" "hash" "io" + "math" "os" "path/filepath" "slices" @@ -768,8 +769,25 @@ func (cdm *ChunkDiskMapper) Chunk(ref ChunkDiskMapperRef) (chunkenc.Chunk, error } } + if chkDataLen > uint64(math.MaxInt) { + return nil, &CorruptionErr{ + Dir: cdm.dir.Name(), + FileIndex: sgmIndex, + Err: fmt.Errorf("chunk length %d exceeds supported size", chkDataLen), + } + } + + chkDataLenInt := int(chkDataLen) + if chkDataLenStart > math.MaxInt-n-chkDataLenInt { + return nil, &CorruptionErr{ + Dir: cdm.dir.Name(), + FileIndex: sgmIndex, + Err: fmt.Errorf("chunk data end overflows supported size (start=%d, len=%d, n=%d)", chkDataLenStart, chkDataLenInt, n), + } + } + // Verify the chunk data end. - chkDataEnd := chkDataLenStart + n + int(chkDataLen) + chkDataEnd := chkDataLenStart + n + chkDataLenInt if chkDataEnd > mmapFile.byteSlice.Len() { return nil, &CorruptionErr{ Dir: cdm.dir.Name(),