Skip to content
Merged
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
38 changes: 32 additions & 6 deletions src/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,22 @@ fn typed_le_tag<T: LeBytes>(tag: u64, s: &[T]) -> Value {

/// Decodes an [`IValue`] tree from CBOR bytes produced by [`encode`].
pub fn decode(bytes: &[u8]) -> Result<IValue, CborDecodeError> {
let cbor: Value = ciborium::from_reader(bytes).map_err(|_| CborDecodeError::DecodeError)?;
cbor_to_ivalue(cbor, 0)
decode_with_depth_limit(bytes, true)
}

/// Decodes an [`IValue`] tree, optionally enforcing the standard nesting limit.
pub fn decode_with_depth_limit(bytes: &[u8], limit_depth: bool) -> Result<IValue, CborDecodeError> {
let recursion_limit = if limit_depth {
MAX_DEPTH as usize
} else {
usize::MAX

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?
cant it be an option?
if from_reader_with_recursion_limit must recive usize call it only when you have limit_depth=true

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no
when the flag is turn off we don't want to enforce the default 256 depth , but we want to have MAX_UNIT

};
let cbor: Value = ciborium::de::from_reader_with_recursion_limit(bytes, recursion_limit)
.map_err(|err| match err {
ciborium::de::Error::RecursionLimitExceeded => CborDecodeError::DepthLimitExceeded,
_ => CborDecodeError::DecodeError,
})?;
cbor_to_ivalue(cbor, 0, limit_depth)
}

/// Decodes an [`IValue`] tree from bytes produced by [`encode_compressed`].
Expand All @@ -187,8 +201,8 @@ pub fn decode_compressed(bytes: &[u8]) -> Result<IValue, CborDecodeError> {
decode(&raw)
}

fn cbor_to_ivalue(val: Value, depth: u32) -> Result<IValue, CborDecodeError> {
if depth >= MAX_DEPTH {
fn cbor_to_ivalue(val: Value, depth: u32, limit_depth: bool) -> Result<IValue, CborDecodeError> {
if limit_depth && depth >= MAX_DEPTH {
return Err(CborDecodeError::DepthLimitExceeded);
}
match val {
Expand All @@ -209,7 +223,7 @@ fn cbor_to_ivalue(val: Value, depth: u32) -> Result<IValue, CborDecodeError> {
let hint = arr.len().min(1024);
let mut out = IArray::with_capacity(hint).map_err(|_| CborDecodeError::AllocError)?;
for v in arr {
let iv = cbor_to_ivalue(v, depth + 1)?;
let iv = cbor_to_ivalue(v, depth + 1, limit_depth)?;
out.push(iv).map_err(|_| CborDecodeError::AllocError)?;
}
Ok(out.into())
Expand All @@ -222,7 +236,7 @@ fn cbor_to_ivalue(val: Value, depth: u32) -> Result<IValue, CborDecodeError> {
Value::Text(s) => s,
_ => return Err(CborDecodeError::InvalidValue),
};
let val = cbor_to_ivalue(v, depth + 1)?;
let val = cbor_to_ivalue(v, depth + 1, limit_depth)?;
obj.insert(&key, val)
.map_err(|_| CborDecodeError::AllocError)?;
}
Expand Down Expand Up @@ -427,6 +441,18 @@ mod tests {
assert_eq!(obj.get("b").unwrap().as_string().unwrap().as_str(), "text");
}

#[test]
fn test_optional_depth_limit() {
let mut value = IValue::NULL;
for _ in 0..300 {
value = IArray::try_from(vec![value]).unwrap().into();
}

let bytes = encode(&value);
assert_eq!(decode(&bytes), Err(CborDecodeError::DepthLimitExceeded));
assert_eq!(decode_with_depth_limit(&bytes, false).unwrap(), value);
}

#[test]
fn test_compressed_round_trip() {
let seed = IValueDeserSeed::new(Some(crate::FPHAConfig::new_with_type(
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ pub use value::{
pub mod cbor;
mod de;
mod ser;
pub use cbor::{decode, decode_compressed, encode, encode_compressed, CborDecodeError};
pub use cbor::{
decode, decode_compressed, decode_with_depth_limit, encode, encode_compressed, CborDecodeError,
};
pub use de::{from_value, FPHAConfig, IValueDeserSeed};
pub use ser::to_value;

Expand Down
Loading