about summary refs log tree commit diff
path: root/users/zseri/store-ref-scanner/src/hbm.rs
diff options
context:
space:
mode:
authorzseri <zseri.devel@ytrizja.de>2021-12-25T02·17+0100
committerzseri <zseri.devel@ytrizja.de>2021-12-25T05·03+0000
commitf4dddea4c375dd0dc472d8879cc4fc506dd77d8c (patch)
tree544f2b746743d35501d978396fe6ba5ad53dbdf2 /users/zseri/store-ref-scanner/src/hbm.rs
parent5f2b37bdb07f5b931ac555939710ce681472e04d (diff)
fix(zseri/store-ref-scanner): no_std support and runtime panics r/3385
This also changes the fuzzing infrastructure from proptest to cargo-fuzz,
and this lead to the discovery of two mishandlings of edge-cases:

* when a "path_to_store" is at the end of the input, it tried to access
  the input slice out-of-bounds (the `just_store` test covers that now)
* non-ASCII characters lead to an out-of-bounds access in HalfBytesMask
  (the `non_ascii` test covers that now)

Change-Id: Icaa2518dcd93e1789a2c0da4cf0fec46016d3bad
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4604
Tested-by: BuildkiteCI
Reviewed-by: zseri <zseri.devel@ytrizja.de>
Diffstat (limited to 'users/zseri/store-ref-scanner/src/hbm.rs')
-rw-r--r--users/zseri/store-ref-scanner/src/hbm.rs35
1 files changed, 23 insertions, 12 deletions
diff --git a/users/zseri/store-ref-scanner/src/hbm.rs b/users/zseri/store-ref-scanner/src/hbm.rs
index 881f1dfdeb75..c2fd2950d5f1 100644
--- a/users/zseri/store-ref-scanner/src/hbm.rs
+++ b/users/zseri/store-ref-scanner/src/hbm.rs
@@ -1,8 +1,7 @@
 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
 pub struct HalfBytesMask(pub [u8; 16]);
 
-// fires erronously
-#[allow(clippy::zero_prefixed_literal)]
+#[allow(clippy::as_conversions, clippy::zero_prefixed_literal)]
 impl HalfBytesMask {
     pub const B32_REVSHA256: HalfBytesMask =
         HalfBytesMask([0, 0, 0, 0, 0, 0, 255, 3, 0, 0, 0, 0, 222, 127, 207, 7]);
@@ -11,6 +10,10 @@ impl HalfBytesMask {
         0, 0, 0, 0, 0, 8, 255, 3, 254, 255, 255, 135, 254, 255, 255, 7,
     ]);
 
+    pub const DFL_REST: HalfBytesMask = HalfBytesMask([
+        0, 0, 0, 0, 0, 104, 255, 163, 254, 255, 255, 135, 254, 255, 255, 7,
+    ]);
+
     #[inline]
     #[proc_unroll::unroll]
     pub const fn from_expanded(x: [bool; 128]) -> Self {
@@ -51,7 +54,11 @@ impl HalfBytesMask {
     }
 
     pub fn contains(&self, byte: u8) -> bool {
-        (self.0[usize::from(byte / 8)] >> u32::from(byte % 8)) & 0b1 != 0
+        if byte >= 0x80 {
+            false
+        } else {
+            (self.0[usize::from(byte / 8)] >> u32::from(byte % 8)) & 0b1 != 0
+        }
     }
 
     pub fn set(&mut self, byte: u8, allow: bool) {
@@ -96,6 +103,13 @@ mod tests {
     }
 
     #[test]
+    fn non_ascii() {
+        for i in 0x80..=0xff {
+            assert!(!HalfBytesMask::DFL_REST.contains(i));
+        }
+    }
+
+    #[test]
     fn dflmask() {
         assert_eq!(
             HalfBytesMask::from_expanded(
@@ -138,15 +152,12 @@ mod tests {
             ),
             HalfBytesMask::B64_BLAKE2B256,
         );
-    }
 
-    proptest::proptest! {
-        #[test]
-        fn hbm_roundtrip(s: [u8; 16]) {
-            let a = HalfBytesMask(s);
-            let b = a.into_expanded();
-            let c = HalfBytesMask::from_expanded(b);
-            assert_eq!(a, c);
-        }
+        assert_eq!(
+            HalfBytesMask::from_bytes(
+                b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-._?="
+            ),
+            HalfBytesMask::DFL_REST,
+        );
     }
 }