Mercurial > hg-stable
diff rust/hg-core/src/revlog/index.rs @ 45605:1cef583541c0
hg-core: return Err if `offset != bytes.len()`
(D8958#inline-14994 followup 2/2)
[X] make `Index` owner of its bytes
[X] make `Index::new` return an error if `offset != bytes.len()`
Differential Revision: https://phab.mercurial-scm.org/D9107
author | Antoine cezar<acezar@chwitlabs.fr> |
---|---|
date | Mon, 28 Sep 2020 15:21:56 +0200 |
parents | 900b9b79b99c |
children | 88e741bf2d93 |
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/index.rs Mon Sep 28 15:13:51 2020 +0200 +++ b/rust/hg-core/src/revlog/index.rs Mon Sep 28 15:21:56 2020 +0200 @@ -2,6 +2,7 @@ use byteorder::{BigEndian, ByteOrder}; +use crate::revlog::revlog::RevlogError; use crate::revlog::{Revision, NULL_REVISION}; pub const INDEX_ENTRY_SIZE: usize = 64; @@ -17,7 +18,9 @@ impl Index { /// Create an index from bytes. /// Calculate the start of each entry when is_inline is true. - pub fn new(bytes: Box<dyn Deref<Target = [u8]> + Send>) -> Self { + pub fn new( + bytes: Box<dyn Deref<Target = [u8]> + Send>, + ) -> Result<Self, RevlogError> { if is_inline(&bytes) { let mut offset: usize = 0; let mut offsets = Vec::new(); @@ -33,15 +36,19 @@ offset += INDEX_ENTRY_SIZE + entry.compressed_len(); } - Self { - bytes, - offsets: Some(offsets), + if offset == bytes.len() { + Ok(Self { + bytes, + offsets: Some(offsets), + }) + } else { + Err(RevlogError::Corrupted) } } else { - Self { + Ok(Self { bytes, offsets: None, - } + }) } }