forge
Explore
Log in

Don't you dare go Hollow

U20: ec-inflate raw-only entry point removed (callers use inflate_into with Format::Raw; no external user); slice-to-array expects replaced by first_chunk

Browse files at this commit

76bd03756c7c45b691e3381029855157e4d46884Ahmet Kaan GümüşParent: 63cd8d5
crates/ec-inflate/fuzz/fuzz_targets/inflate_diff.rsmodified+3 -2
@@ -1,6 +1,6 @@
1 #![no_main]1 #![no_main]
2 2
3-use ec_inflate::{inflate, inflate_zlib};3+use ec_inflate::{Format, inflate_into, inflate_zlib};
4 use libfuzzer_sys::fuzz_target;4 use libfuzzer_sys::fuzz_target;
5 5
6 const LIMIT: usize = 16 << 20;6 const LIMIT: usize = 16 << 20;
@@ -12,8 +12,9 @@ fuzz_target!(|data: &[u8]| {
12 return;12 return;
13 };13 };
14 let (ours, theirs) = if sel & 1 == 0 {14 let (ours, theirs) = if sel & 1 == 0 {
15+ let mut out = Vec::new();
15 (16 (
16- inflate(body, LIMIT),17+ inflate_into(body, &mut out, LIMIT, Format::Raw).map(|()| out),
17 miniz_oxide::inflate::decompress_to_vec_with_limit(body, LIMIT),18 miniz_oxide::inflate::decompress_to_vec_with_limit(body, LIMIT),
18 )19 )
19 } else {20 } else {
crates/ec-inflate/fuzz/fuzz_targets/inflate_raw.rsmodified+2 -2
@@ -1,9 +1,9 @@
1 #![no_main]1 #![no_main]
2 2
3-use ec_inflate::inflate;3+use ec_inflate::{Format, inflate_into};
4 use libfuzzer_sys::fuzz_target;4 use libfuzzer_sys::fuzz_target;
5 5
6 // Whole input is one raw DEFLATE stream, capped at 16 MiB of output.6 // Whole input is one raw DEFLATE stream, capped at 16 MiB of output.
7 fuzz_target!(|data: &[u8]| {7 fuzz_target!(|data: &[u8]| {
8- let _ = inflate(data, 16 << 20);8+ let _ = inflate_into(data, &mut Vec::new(), 16 << 20, Format::Raw);
9 });9 });
crates/ec-inflate/src/bits.rsmodified+2 -2
@@ -39,8 +39,8 @@ impl<'a> Bits<'a> {
39 /// most symbols.39 /// most symbols.
40 #[inline]40 #[inline]
41 pub(crate) fn refill(&mut self) {41 pub(crate) fn refill(&mut self) {
42- if let Some(next) = self.src.get(self.pos..self.pos + 8) {42+ if let Some(next) = self.src.get(self.pos..).and_then(<[u8]>::first_chunk::<8>) {
43- let bytes = u64::from_le_bytes(next.try_into().expect("an eight byte slice"));43+ let bytes = u64::from_le_bytes(*next);
44 self.buf |= bytes << self.count;44 self.buf |= bytes << self.count;
45 self.pos += ((63 - self.count) >> 3) as usize;45 self.pos += ((63 - self.count) >> 3) as usize;
46 self.count |= 56;46 self.count |= 56;
crates/ec-inflate/src/deflate.rsmodified+6 -1
@@ -205,7 +205,7 @@ fn stored_blocks(input: &[u8]) -> Vec<u8> {
205 #[cfg(test)]205 #[cfg(test)]
206 mod tests {206 mod tests {
207 use super::*;207 use super::*;
208- use crate::{inflate, inflate_zlib};208+ use crate::inflate_zlib;
209 209
210 fn pseudo_random(n: usize, seed: u32) -> Vec<u8> {210 fn pseudo_random(n: usize, seed: u32) -> Vec<u8> {
211 let mut s = seed;211 let mut s = seed;
@@ -217,6 +217,11 @@ mod tests {
217 .collect()217 .collect()
218 }218 }
219 219
220+ fn inflate(raw: &[u8], limit: usize) -> Result<Vec<u8>, crate::Error> {
221+ let mut out = Vec::new();
222+ crate::inflate_into(raw, &mut out, limit, crate::Format::Raw).map(|()| out)
223+ }
224+
220 #[test]225 #[test]
221 fn empty_and_tiny_inputs_round_trip() {226 fn empty_and_tiny_inputs_round_trip() {
222 for input in [&b""[..], b"a", b"ab", b"abc", b"abcabcabc"] {227 for input in [&b""[..], b"a", b"ab", b"abc", b"abcabcabc"] {
crates/ec-inflate/src/lib.rsmodified+8 -17
@@ -1,9 +1,9 @@
1 //! DEFLATE (RFC 1951) and zlib (RFC 1950) decompression.1 //! DEFLATE (RFC 1951) and zlib (RFC 1950) decompression.
2 //!2 //!
3-//! Two entry points, both one-shot over a complete buffer: [`inflate`] for a3+//! Two entry points, both one-shot over a complete buffer: [`inflate_zlib`] for a
4-//! raw DEFLATE stream (what PNG's `IDAT` chain and Matroska header stripping4+//! zlib stream, header checked and Adler-32 verified, and [`inflate_into`] for
5-//! hand over) and [`inflate_zlib`] for the same thing inside a zlib wrapper,5+//! either wrapper, with [`Format::Raw`] for a bare DEFLATE stream (what
6-//! header checked and Adler-32 verified.6+//! Matroska header stripping hands over).
7 //!7 //!
8 //! Contracts worth knowing before implementing against this crate:8 //! Contracts worth knowing before implementing against this crate:
9 //!9 //!
@@ -117,13 +117,6 @@ pub enum Format {
117 Zlib,117 Zlib,
118 }118 }
119 119
120-/// Decompress a raw DEFLATE stream, producing at most `limit` bytes.
121-pub fn inflate(input: &[u8], limit: usize) -> Result<Vec<u8>, Error> {
122- let mut out = Vec::new();
123- inflate_into(input, &mut out, limit, Format::Raw)?;
124- Ok(out)
125-}
126-
127 /// Decompress a zlib stream, producing at most `limit` bytes.120 /// Decompress a zlib stream, producing at most `limit` bytes.
128 ///121 ///
129 /// The two header bytes are checked and the Adler-32 trailer is verified122 /// The two header bytes are checked and the Adler-32 trailer is verified
@@ -153,13 +146,11 @@ pub fn inflate_into(
153 let mut bits = Bits::new(body);146 let mut bits = Bits::new(body);
154 let end = inflate::blocks(&mut bits, out, limit)?;147 let end = inflate::blocks(&mut bits, out, limit)?;
155 if format == Format::Zlib {148 if format == Format::Zlib {
156- let trailer: [u8; 4] = body149+ let trailer = body
157 .get(end..)150 .get(end..)
158- .and_then(|rest| rest.get(..4))151+ .and_then(<[u8]>::first_chunk::<4>)
159- .ok_or(Error::Truncated)?152+ .ok_or(Error::Truncated)?;
160- .try_into()153+ let expected = u32::from_be_bytes(*trailer);
161- .expect("a four byte slice");
162- let expected = u32::from_be_bytes(trailer);
163 let actual = adler32(&out[start..]);154 let actual = adler32(&out[start..]);
164 if expected != actual {155 if expected != actual {
165 return Err(Error::Adler32Mismatch { expected, actual });156 return Err(Error::Adler32Mismatch { expected, actual });
crates/ec-inflate/tests/inflate.rsmodified+6 -1
@@ -7,12 +7,17 @@
7 7
8 use std::io::Write;8 use std::io::Write;
9 9
10-use ec_inflate::{Error, Format, inflate, inflate_into, inflate_zlib};10+use ec_inflate::{Error, Format, inflate_into, inflate_zlib};
11 use flate2::Compression;11 use flate2::Compression;
12 use flate2::write::{DeflateEncoder, ZlibEncoder};12 use flate2::write::{DeflateEncoder, ZlibEncoder};
13 13
14 const NO_LIMIT: usize = usize::MAX;14 const NO_LIMIT: usize = usize::MAX;
15 15
16+fn inflate(input: &[u8], limit: usize) -> Result<Vec<u8>, Error> {
17+ let mut out = Vec::new();
18+ inflate_into(input, &mut out, limit, Format::Raw).map(|()| out)
19+}
20+
16 /// xorshift64*, so the "random" inputs are the same on every run.21 /// xorshift64*, so the "random" inputs are the same on every run.
17 fn pseudo_random(seed: u64, len: usize) -> Vec<u8> {22 fn pseudo_random(seed: u64, len: usize) -> Vec<u8> {
18 let mut state = seed | 1;23 let mut state = seed | 1;