rust-index: return an error on a bad index header
authorRaphaël Gomès <rgomes@octobus.net>
Thu, 10 Oct 2024 10:38:10 +0200
changeset 52162 f2eab4967bfc
parent 52161 46c68c0fe137
child 52163 44823c5011fe
rust-index: return an error on a bad index header This is more idiomatic and allows us to better handle the problem later.
rust/hg-core/src/revlog/index.rs
rust/hg-cpython/src/revlog.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<Option<IndexHeader>, HgError> {
-        if index_bytes.is_empty() {
-            return Ok(None);
-        }
+    pub fn parse(index_bytes: &[u8]) -> Result<IndexHeader, HgError> {
         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<dyn Deref<Target = [u8]> + Send + Sync>,
         default_header: IndexHeader,
     ) -> Result<Self, HgError> {
-        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()
     }
 
--- 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())