From 01af11f3403ff0f96d7100c5bca72a4cb55c2562 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 29 May 2019 07:02:45 +0200 Subject: [PATCH] src/backup/fixed_index.rs: improve consistency checks --- src/backup/fixed_index.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/backup/fixed_index.rs b/src/backup/fixed_index.rs index be599a34..dcea9932 100644 --- a/src/backup/fixed_index.rs +++ b/src/backup/fixed_index.rs @@ -318,11 +318,9 @@ impl FixedIndexWriter { &self.stat } - // Note: We want to add data out of order, so do not assume and order here. + // Note: We want to add data out of order, so do not assume any order here. pub fn add_chunk(&mut self, pos: usize, chunk: &[u8]) -> Result<(), Error> { - if self.index == std::ptr::null_mut() { bail!("cannot write to closed index file."); } - let end = pos + chunk.len(); if end > self.size { @@ -335,11 +333,8 @@ impl FixedIndexWriter { bail!("got chunk with wrong length ({} != {}", chunk.len(), self.chunk_size); } - if pos >= self.size { bail!("add chunk after end ({} >= {})", pos, self.size); } - if pos & (self.chunk_size-1) != 0 { bail!("add unaligned chunk (pos = {})", pos); } - let (is_duplicate, digest, compressed_size) = self.store.insert_chunk(chunk)?; self.stat.chunk_count += 1; @@ -358,10 +353,13 @@ impl FixedIndexWriter { } pub fn add_digest(&mut self, index: usize, digest: &[u8; 32]) -> Result<(), Error> { + if index >= self.index_length { bail!("add digest failed - index out of range ({} >= {})", index, self.index_length); } + if self.index == std::ptr::null_mut() { bail!("cannot write to closed index file."); } + let index_pos = index*32; unsafe { let dst = self.index.add(index_pos);