# HG changeset patch # User Raphaël Gomès # Date 1728549490 -7200 # Node ID f2eab4967bfcb3016a635f3b980006e5b5e8408a # Parent 46c68c0fe137ad05dd86035f86e4ff2aa5c28f9d rust-index: return an error on a bad index header This is more idiomatic and allows us to better handle the problem later. diff -r 46c68c0fe137 -r f2eab4967bfc rust/hg-core/src/revlog/index.rs --- a/rust/hg-core/src/revlog/index.rs Thu Oct 17 15:22:38 2024 +0200 +++ b/rust/hg-core/src/revlog/index.rs Thu Oct 10 10:38:10 2024 +0200 @@ -62,16 +62,13 @@ BigEndian::read_u16(&self.header_bytes[2..4]) } - pub fn parse(index_bytes: &[u8]) -> Result, HgError> { - if index_bytes.is_empty() { - return Ok(None); - } + pub fn parse(index_bytes: &[u8]) -> Result { if index_bytes.len() < 4 { return Err(HgError::corrupted( "corrupted revlog: can't read the index format header", )); } - Ok(Some(IndexHeader { + Ok(IndexHeader { header_bytes: { let bytes: [u8; 4] = index_bytes[0..4].try_into().expect("impossible"); @@ -341,8 +338,11 @@ bytes: Box + Send + Sync>, default_header: IndexHeader, ) -> Result { - let header = - IndexHeader::parse(bytes.as_ref())?.unwrap_or(default_header); + let header = if bytes.len() < INDEX_ENTRY_SIZE { + default_header + } else { + IndexHeader::parse(bytes.as_ref())? + }; if header.format_version() != IndexHeader::REVLOGV1 { // A proper new version should have had a repo/store @@ -1901,24 +1901,21 @@ pub fn is_inline(index_bytes: &[u8]) -> bool { IndexHeader::parse(index_bytes) - .expect("too short") - .unwrap() + .expect("invalid header") .format_flags() .is_inline() } pub fn uses_generaldelta(index_bytes: &[u8]) -> bool { IndexHeader::parse(index_bytes) - .expect("too short") - .unwrap() + .expect("invalid header") .format_flags() .uses_generaldelta() } pub fn get_version(index_bytes: &[u8]) -> u16 { IndexHeader::parse(index_bytes) - .expect("too short") - .unwrap() + .expect("invalid header") .format_version() } diff -r 46c68c0fe137 -r f2eab4967bfc rust/hg-cpython/src/revlog.rs --- a/rust/hg-cpython/src/revlog.rs Thu Oct 17 15:22:38 2024 +0200 +++ b/rust/hg-cpython/src/revlog.rs Thu Oct 10 10:38:10 2024 +0200 @@ -632,8 +632,7 @@ hg::index::Index::new( bytes, IndexHeader::parse(&header.to_be_bytes()) - .expect("default header is broken") - .unwrap(), + .expect("default header is broken"), ) .map_err(|e| { revlog_error_with_msg(py, e.to_string().as_bytes())