From 587276c000a75179b83630ad74938dfe05c26777 Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Fri, 13 Sep 2024 20:21:02 -0700 Subject: [PATCH] read/elf: Read zstd frames in a loop until decompression is complete. (#730) --- src/read/mod.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/read/mod.rs b/src/read/mod.rs index 0b6e0c28..eaf733f1 100644 --- a/src/read/mod.rs +++ b/src/read/mod.rs @@ -994,13 +994,30 @@ impl<'data> CompressedData<'data> { .read_error("Invalid zlib compressed data")?; } CompressionFormat::Zstandard => { - let mut decoder = ruzstd::StreamingDecoder::new(self.data) - .ok() - .read_error("Invalid zstd compressed data")?; - decoder - .read_to_end(&mut decompressed) - .ok() - .read_error("Invalid zstd compressed data")?; + let mut input = self.data; + while !input.is_empty() { + let mut decoder = match ruzstd::StreamingDecoder::new(&mut input) { + Ok(decoder) => decoder, + Err( + ruzstd::frame_decoder::FrameDecoderError::ReadFrameHeaderError( + ruzstd::frame::ReadFrameHeaderError::SkipFrame { + length, + .. + }, + ), + ) => { + input = &input + .get(length as usize..) + .read_error("Invalid zstd compressed data")?; + continue; + } + x => x.ok().read_error("Invalid zstd compressed data")?, + }; + decoder + .read_to_end(&mut decompressed) + .ok() + .read_error("Invalid zstd compressed data")?; + } } _ => unreachable!(), }