Skip to content

Commit a9c26c5

Browse files
committed
add some asserts
1 parent abaa002 commit a9c26c5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/segment/writer.rs

+15
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,30 @@ impl Writer {
8383
/// # Errors
8484
///
8585
/// Will return `Err` if an IO error occurs.
86+
///
87+
/// # Panics
88+
///
89+
/// Panics if the key length is empty or greater than 2^16, or the value length is greater than 2^32.
8690
pub fn write(&mut self, key: &[u8], value: &[u8]) -> std::io::Result<u32> {
91+
assert!(!key.is_empty());
92+
assert!(key.len() <= u16::MAX.into());
93+
assert!(u32::try_from(value.len()).is_ok());
94+
8795
#[cfg(feature = "lz4")]
8896
let value = lz4_flex::compress_prepend_size(value);
8997

9098
let mut hasher = crc32fast::Hasher::new();
9199
hasher.update(&value);
92100
let crc = hasher.finalize();
93101

102+
// NOTE: Truncation is okay and actually needed
103+
#[allow(clippy::cast_possible_truncation)]
94104
self.inner.write_u16::<BigEndian>(key.len() as u16)?;
95105
self.inner.write_all(key)?;
96106
self.inner.write_u32::<BigEndian>(crc)?;
107+
108+
// NOTE: Truncation is okay and actually needed
109+
#[allow(clippy::cast_possible_truncation)]
97110
self.inner.write_u32::<BigEndian>(value.len() as u32)?;
98111
self.inner.write_all(&value)?;
99112

@@ -112,6 +125,8 @@ impl Writer {
112125

113126
self.item_count += 1;
114127

128+
// NOTE: Truncation is okay
129+
#[allow(clippy::cast_possible_truncation)]
115130
Ok(value.len() as u32)
116131
}
117132

0 commit comments

Comments
 (0)