[Updated] [+- ] D9107: hg-core: return Err if `offset != bytes.len()` (D8958#inline-14994 followup 2/2)

acezar (Antoine Cezar) phabricator at mercurial-scm.org
Tue Sep 29 08:20:43 UTC 2020


acezar updated this revision to Diff 22911.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D9107?vs=22893&id=22911

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D9107/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D9107

AFFECTED FILES
  rust/hg-core/src/revlog/index.rs
  rust/hg-core/src/revlog/revlog.rs

CHANGE DETAILS

diff --git a/rust/hg-core/src/revlog/revlog.rs b/rust/hg-core/src/revlog/revlog.rs
--- a/rust/hg-core/src/revlog/revlog.rs
+++ b/rust/hg-core/src/revlog/revlog.rs
@@ -56,7 +56,7 @@
             return Err(RevlogError::UnsuportedVersion(version));
         }
 
-        let index = Index::new(Box::new(index_mmap));
+        let index = Index::new(Box::new(index_mmap))?;
 
         // TODO load data only when needed //
         // type annotation required
diff --git a/rust/hg-core/src/revlog/index.rs b/rust/hg-core/src/revlog/index.rs
--- a/rust/hg-core/src/revlog/index.rs
+++ b/rust/hg-core/src/revlog/index.rs
@@ -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,
-            }
+            })
         }
     }
 



To: acezar, #hg-reviewers, Alphare
Cc: Alphare, mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20200929/f7c1dd57/attachment-0002.html>


More information about the Mercurial-patches mailing list