Skip to content

Commit 88a2b2d

Browse files
authored
Merge pull request #172 from rust-secure-code/upgrade-and-insulate-miniz-oxide
Upgrade and insulate miniz oxide
2 parents e89342f + bf9ab62 commit 88a2b2d

6 files changed

Lines changed: 87 additions & 21 deletions

File tree

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auditable-info/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2018"
1212

1313
[dependencies]
1414
auditable-extract = {version = "0.3.4", path = "../auditable-extract", default-features = false }
15-
miniz_oxide = { version = "0.6.2", features = ["std"] }
15+
miniz_oxide = { version = "0.8.0", features = ["std"] }
1616
auditable-serde = {version = "0.7.0", path = "../auditable-serde", optional = true}
1717
serde_json = { version = "1.0.57", optional = true }
1818

auditable-info/src/error.rs

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub enum Error {
55
OutputLimitExceeded,
66
Io(std::io::Error),
77
BinaryParsing(auditable_extract::Error),
8-
Decompression(miniz_oxide::inflate::DecompressError),
8+
Decompression(DecompressError),
99
#[cfg(feature = "serde")]
1010
Json(serde_json::Error),
1111
Utf8(std::str::Utf8Error),
@@ -58,10 +58,10 @@ impl From<auditable_extract::Error> for Error {
5858
}
5959
}
6060

61-
impl From<miniz_oxide::inflate::DecompressError> for Error {
62-
fn from(e: miniz_oxide::inflate::DecompressError) -> Self {
61+
impl From<DecompressError> for Error {
62+
fn from(e: DecompressError) -> Self {
6363
match e.status {
64-
miniz_oxide::inflate::TINFLStatus::HasMoreOutput => Error::OutputLimitExceeded,
64+
TINFLStatus::HasMoreOutput => Error::OutputLimitExceeded,
6565
_ => Error::Decompression(e),
6666
}
6767
}
@@ -79,3 +79,67 @@ impl From<serde_json::Error> for Error {
7979
Self::Json(e)
8080
}
8181
}
82+
83+
/// A copy of [miniz_oxide::inflate::DecompressError].
84+
///
85+
/// We use our copy instead of the miniz_oxide type directly
86+
/// so that we don't have to bump semver every time `miniz_oxide` does.
87+
#[derive(Debug)]
88+
pub struct DecompressError {
89+
/// Decompressor status on failure. See [TINFLStatus] for details.
90+
pub status: TINFLStatus,
91+
/// The currently decompressed data if any.
92+
pub output: Vec<u8>,
93+
}
94+
95+
impl std::fmt::Display for DecompressError {
96+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
97+
f.write_str(match self.status {
98+
TINFLStatus::FailedCannotMakeProgress => "Truncated input stream",
99+
TINFLStatus::BadParam => "Invalid output buffer size",
100+
TINFLStatus::Adler32Mismatch => "Adler32 checksum mismatch",
101+
TINFLStatus::Failed => "Invalid input data",
102+
TINFLStatus::Done => unreachable!(),
103+
TINFLStatus::NeedsMoreInput => "Truncated input stream",
104+
TINFLStatus::HasMoreOutput => "Output size exceeded the specified limit",
105+
})
106+
}
107+
}
108+
109+
impl std::error::Error for DecompressError {}
110+
111+
impl DecompressError {
112+
pub(crate) fn from_miniz(err: miniz_oxide::inflate::DecompressError) -> Self {
113+
Self {
114+
status: TINFLStatus::from_miniz(err.status),
115+
output: err.output,
116+
}
117+
}
118+
}
119+
120+
#[repr(i8)]
121+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
122+
pub enum TINFLStatus {
123+
FailedCannotMakeProgress,
124+
BadParam,
125+
Adler32Mismatch,
126+
Failed,
127+
Done,
128+
NeedsMoreInput,
129+
HasMoreOutput,
130+
}
131+
132+
impl TINFLStatus {
133+
pub(crate) fn from_miniz(status: miniz_oxide::inflate::TINFLStatus) -> Self {
134+
use miniz_oxide::inflate;
135+
match status {
136+
inflate::TINFLStatus::FailedCannotMakeProgress => Self::FailedCannotMakeProgress,
137+
inflate::TINFLStatus::BadParam => Self::BadParam,
138+
inflate::TINFLStatus::Adler32Mismatch => Self::Adler32Mismatch,
139+
inflate::TINFLStatus::Failed => Self::Failed,
140+
inflate::TINFLStatus::Done => Self::Done,
141+
inflate::TINFLStatus::NeedsMoreInput => Self::NeedsMoreInput,
142+
inflate::TINFLStatus::HasMoreOutput => Self::HasMoreOutput,
143+
}
144+
}
145+
}

auditable-info/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::path::Path;
2828

2929
mod error;
3030

31-
pub use crate::error::Error;
31+
pub use crate::error::*;
3232

3333
/// Loads audit info from the specified binary compiled with `cargo auditable`.
3434
///
@@ -82,7 +82,8 @@ pub fn audit_info_from_reader<T: BufRead>(
8282
pub fn json_from_reader<T: BufRead>(reader: &mut T, limits: Limits) -> Result<String, Error> {
8383
let compressed_data = get_compressed_audit_data(reader, limits)?;
8484
let decompressed_data =
85-
decompress_to_vec_zlib_with_limit(&compressed_data, limits.decompressed_json_size)?;
85+
decompress_to_vec_zlib_with_limit(&compressed_data, limits.decompressed_json_size)
86+
.map_err(DecompressError::from_miniz)?;
8687
Ok(String::from_utf8(decompressed_data)?)
8788
}
8889

@@ -144,7 +145,8 @@ pub fn json_from_slice(
144145
Err(Error::OutputLimitExceeded)?;
145146
}
146147
let decompressed_data =
147-
decompress_to_vec_zlib_with_limit(compressed_audit_data, decompressed_json_size_limit)?;
148+
decompress_to_vec_zlib_with_limit(compressed_audit_data, decompressed_json_size_limit)
149+
.map_err(DecompressError::from_miniz)?;
148150
Ok(String::from_utf8(decompressed_data)?)
149151
}
150152

cargo-auditable/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ readme = "../README.md"
1515
[dependencies]
1616
object = {version = "0.30", default-features = false, features = ["write"]}
1717
auditable-serde = {version = "0.7.0", path = "../auditable-serde", features = ["from_metadata"]}
18-
miniz_oxide = {version = "0.6.0"}
18+
miniz_oxide = {version = "0.8.0"}
1919
serde_json = "1.0.57"
2020
cargo_metadata = "0.18"
2121
pico-args = "0.5"

rust-audit-info/Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)