From 4693f73fbf44ab8c90516dc83883b5968de7ed3e Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Tue, 30 Apr 2024 17:10:32 +0200 Subject: [PATCH 1/2] feat: ensure offset size is at most 8 bytes --- crates/storage/nippy-jar/src/error.rs | 5 +++++ crates/storage/nippy-jar/src/lib.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/crates/storage/nippy-jar/src/error.rs b/crates/storage/nippy-jar/src/error.rs index c769f0db8630..3763be3dcfe1 100644 --- a/crates/storage/nippy-jar/src/error.rs +++ b/crates/storage/nippy-jar/src/error.rs @@ -37,6 +37,11 @@ pub enum NippyJarError { PHFMissing, #[error("nippy jar was built without an index")] UnsupportedFilterQuery, + #[error("the size of an offset must be at most 8 bytes, got {offset_size}")] + OffsetSizeTooBig { + /// The read offset size in number of bytes. + offset_size: u64, + }, #[error("compression or decompression requires a bigger destination output")] OutputTooSmall, #[error("dictionary is not loaded.")] diff --git a/crates/storage/nippy-jar/src/lib.rs b/crates/storage/nippy-jar/src/lib.rs index cc4f2b0f5147..4623a9284752 100644 --- a/crates/storage/nippy-jar/src/lib.rs +++ b/crates/storage/nippy-jar/src/lib.rs @@ -485,6 +485,12 @@ impl DataReader { let offset_file = File::open(path.as_ref().with_extension(OFFSETS_FILE_EXTENSION))?; // SAFETY: File is read-only and its descriptor is kept alive as long as the mmap handle. let offset_mmap = unsafe { Mmap::map(&offset_file)? }; + let offset_size = offset_mmap[0] as u64; + + // Ensure that the size of an offset is at most 8 bytes. + if offset_size > 8 { + return Err(NippyJarError::OffsetSizeTooBig { offset_size }) + } Ok(Self { data_file, From 046b389b91871214a198c11b08710ec0f94d5e6c Mon Sep 17 00:00:00 2001 From: Oliver Nordbjerg Date: Tue, 30 Apr 2024 17:20:46 +0200 Subject: [PATCH 2/2] chore: nits --- crates/storage/nippy-jar/src/lib.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/crates/storage/nippy-jar/src/lib.rs b/crates/storage/nippy-jar/src/lib.rs index 4623a9284752..59fc586e4b39 100644 --- a/crates/storage/nippy-jar/src/lib.rs +++ b/crates/storage/nippy-jar/src/lib.rs @@ -485,6 +485,8 @@ impl DataReader { let offset_file = File::open(path.as_ref().with_extension(OFFSETS_FILE_EXTENSION))?; // SAFETY: File is read-only and its descriptor is kept alive as long as the mmap handle. let offset_mmap = unsafe { Mmap::map(&offset_file)? }; + + // First byte is the size of one offset in bytes let offset_size = offset_mmap[0] as u64; // Ensure that the size of an offset is at most 8 bytes. @@ -492,14 +494,7 @@ impl DataReader { return Err(NippyJarError::OffsetSizeTooBig { offset_size }) } - Ok(Self { - data_file, - data_mmap, - offset_file, - // First byte is the size of one offset in bytes - offset_size: offset_mmap[0] as u64, - offset_mmap, - }) + Ok(Self { data_file, data_mmap, offset_file, offset_size, offset_mmap }) } /// Returns the offset for the requested data index