From a9c26c56a53b30477ade4b11d54db6daa35d7f98 Mon Sep 17 00:00:00 2001 From: marvin-j97 Date: Wed, 13 Mar 2024 22:32:05 +0100 Subject: [PATCH] add some asserts --- src/segment/writer.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/segment/writer.rs b/src/segment/writer.rs index 3d2961f..92b0867 100644 --- a/src/segment/writer.rs +++ b/src/segment/writer.rs @@ -83,7 +83,15 @@ impl Writer { /// # Errors /// /// Will return `Err` if an IO error occurs. + /// + /// # Panics + /// + /// Panics if the key length is empty or greater than 2^16, or the value length is greater than 2^32. pub fn write(&mut self, key: &[u8], value: &[u8]) -> std::io::Result { + assert!(!key.is_empty()); + assert!(key.len() <= u16::MAX.into()); + assert!(u32::try_from(value.len()).is_ok()); + #[cfg(feature = "lz4")] let value = lz4_flex::compress_prepend_size(value); @@ -91,9 +99,14 @@ impl Writer { hasher.update(&value); let crc = hasher.finalize(); + // NOTE: Truncation is okay and actually needed + #[allow(clippy::cast_possible_truncation)] self.inner.write_u16::(key.len() as u16)?; self.inner.write_all(key)?; self.inner.write_u32::(crc)?; + + // NOTE: Truncation is okay and actually needed + #[allow(clippy::cast_possible_truncation)] self.inner.write_u32::(value.len() as u32)?; self.inner.write_all(&value)?; @@ -112,6 +125,8 @@ impl Writer { self.item_count += 1; + // NOTE: Truncation is okay + #[allow(clippy::cast_possible_truncation)] Ok(value.len() as u32) }