Skip to content

Support unlimit stack depth #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rmp-serde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rmp = { version = "0.8.14", path = "../rmp" }
rmpv = { path = "../rmpv" }
serde_bytes = "0.11.5"
serde = { version = "1.0.197", features = ["derive"] }
serde_stacker = "0.1.12"

[badges]
maintenance = { status = "looking-for-maintainer" }
25 changes: 17 additions & 8 deletions rmp-serde/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ pub enum Error {
macro_rules! depth_count(
( $counter:expr, $expr:expr ) => {
{
$counter -= 1;
if $counter == 0 {
return Err(Error::DepthLimitExceeded)
if let Some(counter) = &mut $counter {
*counter -= 1;
if *counter == 0 {
return Err(Error::DepthLimitExceeded)
}
}
let res = $expr;
$counter += 1;
if let Some(counter) = &mut $counter {
*counter += 1;
}
res
}
}
Expand Down Expand Up @@ -181,7 +185,7 @@ pub struct Deserializer<R, C = DefaultConfig> {
_config: PhantomData<C>,
is_human_readable: bool,
marker: Option<Marker>,
depth: u16,
depth: Option<u16>,
}

impl<R: Read, C> Deserializer<R, C> {
Expand Down Expand Up @@ -213,7 +217,7 @@ impl<R: Read> Deserializer<ReadReader<R>, DefaultConfig> {
is_human_readable: DefaultConfig.is_human_readable(),
// Cached marker in case of deserializing optional values.
marker: None,
depth: 1024,
depth: Some(1024),
}
}
}
Expand Down Expand Up @@ -294,7 +298,7 @@ where
is_human_readable: DefaultConfig.is_human_readable(),
_config: PhantomData,
marker: None,
depth: 1024,
depth: Some(1024),
}
}

Expand All @@ -310,7 +314,12 @@ impl<'de, R: ReadSlice<'de>, C: SerializerConfig> Deserializer<R, C> {
/// Changes the maximum nesting depth that is allowed
#[inline(always)]
pub fn set_max_depth(&mut self, depth: usize) {
self.depth = depth.min(u16::MAX as _) as u16;
self.depth = Some(depth.min(u16::MAX as _) as u16);
}
/// disable stack depth check
#[inline(always)]
pub fn set_depth_unlimit(&mut self) {
self.depth = None;
}
}

Expand Down
29 changes: 29 additions & 0 deletions rmp-serde/tests/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,32 @@ fn fail_depth_limit() {
other => panic!("unexpected result: {other:?}"),
}
}



#[test]
fn depth_unlimit() {
#[allow(dead_code)]
struct Nested {
sub: Vec<Nested>,
}

impl<'de> de::Deserialize<'de> for Nested {
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where D: de::Deserializer<'de>
{
let nested = Vec::deserialize(de)?;
Ok(Nested { sub: nested })
}
}
let mut data = Vec::new();
for _ in 0..(u16::MAX as u32 + 1) {
data.push(0x91u8);
}

let mut reader = rmp_serde::Deserializer::new(Cursor::new(data));
reader.set_depth_unlimit();
let deserializer = serde_stacker::Deserializer::new(&mut reader);
let res = Nested::deserialize(deserializer);
if let decode::Error::DepthLimitExceeded = res.err().unwrap() { panic!("unexpected DepthLimitExceeded") }
}