chore: some fuzz fixes
This commit is contained in:
1 parent
6e94e6dfca
commit
a2b92e6dc5
36 files changed
+1466
-2814
No files matched your search
@@ -51,6 +51,18 @@ impl NbtCompound {
|
||||
|
||||
/// Advances a reader past a compound's payload without allocating its tags.
|
||||
pub fn skip_content<'a, R: NbtReadHelper<'a>>(reader: &mut R) -> Result<(), Error> {
|
||||
Self::skip_content_depth(reader, 0)
|
||||
}
|
||||
|
||||
/// Advances a reader past a compound's payload with depth tracking.
|
||||
pub fn skip_content_depth<'a, R: NbtReadHelper<'a>>(
|
||||
reader: &mut R,
|
||||
depth: usize,
|
||||
) -> Result<(), Error> {
|
||||
if depth > crate::MAX_NBT_DEPTH {
|
||||
return Err(Error::MaxDepthExceeded);
|
||||
}
|
||||
|
||||
loop {
|
||||
let tag_id = match reader.get_u8() {
|
||||
Ok(id) => id,
|
||||
@@ -65,7 +77,7 @@ impl NbtCompound {
|
||||
reader.skip_string()?;
|
||||
|
||||
// Skip Value
|
||||
NbtTag::skip_data(reader, tag_id)?;
|
||||
NbtTag::skip_data_depth(reader, tag_id, depth + 1)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -73,6 +85,18 @@ impl NbtCompound {
|
||||
|
||||
/// Deserializes a compound payload, starting after the compound's name.
|
||||
pub fn deserialize_content<'a, R: NbtReadHelper<'a>>(reader: &mut R) -> Result<Self, Error> {
|
||||
Self::deserialize_content_depth(reader, 0)
|
||||
}
|
||||
|
||||
/// Deserializes a compound payload with depth tracking.
|
||||
pub fn deserialize_content_depth<'a, R: NbtReadHelper<'a>>(
|
||||
reader: &mut R,
|
||||
depth: usize,
|
||||
) -> Result<Self, Error> {
|
||||
if depth > crate::MAX_NBT_DEPTH {
|
||||
return Err(Error::MaxDepthExceeded);
|
||||
}
|
||||
|
||||
let mut compound = Self::new();
|
||||
|
||||
loop {
|
||||
@@ -87,7 +111,7 @@ impl NbtCompound {
|
||||
}
|
||||
|
||||
let name = reader.get_string()?;
|
||||
let tag = NbtTag::deserialize_data(reader, tag_id)?;
|
||||
let tag = NbtTag::deserialize_data_depth(reader, tag_id, depth + 1)?;
|
||||
|
||||
compound.child_tags.insert(name.into(), tag);
|
||||
}
|
||||
|
||||
@@ -63,7 +63,9 @@ pub const INT_ARRAY_ID: u8 = 0x0B;
|
||||
pub const LONG_ARRAY_ID: u8 = 0x0C;
|
||||
|
||||
/// Maximum number of elements accepted when decoding a list or array.
|
||||
pub const MAX_ARRAY_LENGTH: usize = 2_000_000;
|
||||
pub const MAX_ARRAY_LENGTH: usize = 512_000;
|
||||
/// Maximum nesting depth allowed when decoding NBT compound or list tags.
|
||||
pub const MAX_NBT_DEPTH: usize = 512;
|
||||
|
||||
/// Errors produced while reading, writing, or converting NBT data.
|
||||
#[derive(Error, Debug)]
|
||||
@@ -101,6 +103,12 @@ pub enum Error {
|
||||
/// A Bedrock variable-length long exceeded its maximum encoded size.
|
||||
#[error("Failed to decode varlong - value too large")]
|
||||
VarLongTooLarge,
|
||||
/// NBT nesting depth exceeded the maximum allowed limit.
|
||||
#[error("NBT depth exceeded maximum allowed limit")]
|
||||
MaxDepthExceeded,
|
||||
/// A list tag specified an invalid element tag type.
|
||||
#[error("Invalid element tag type for list: {0}")]
|
||||
InvalidListTag(u8),
|
||||
}
|
||||
|
||||
/// A complete NBT document containing a named root compound.
|
||||
|
||||
@@ -6,8 +6,7 @@ use serializer::NbtWriteHelper;
|
||||
|
||||
use crate::{
|
||||
BYTE_ARRAY_ID, BYTE_ID, COMPOUND_ID, DOUBLE_ID, END_ID, Error, FLOAT_ID, INT_ARRAY_ID, INT_ID,
|
||||
LIST_ID, LONG_ARRAY_ID, LONG_ID, MAX_ARRAY_LENGTH, SHORT_ID, STRING_ID, compound, deserializer,
|
||||
serializer,
|
||||
LIST_ID, LONG_ARRAY_ID, LONG_ID, SHORT_ID, STRING_ID, compound, deserializer, serializer,
|
||||
};
|
||||
|
||||
/// A value represented by one of the tag types defined by the NBT format.
|
||||
@@ -203,6 +202,19 @@ impl NbtTag {
|
||||
|
||||
/// Advances a reader past the payload belonging to `tag_id`.
|
||||
pub fn skip_data<'a, R: NbtReadHelper<'a>>(reader: &mut R, tag_id: u8) -> Result<(), Error> {
|
||||
Self::skip_data_depth(reader, tag_id, 0)
|
||||
}
|
||||
|
||||
/// Advances a reader past the payload belonging to `tag_id` with depth tracking.
|
||||
pub fn skip_data_depth<'a, R: NbtReadHelper<'a>>(
|
||||
reader: &mut R,
|
||||
tag_id: u8,
|
||||
depth: usize,
|
||||
) -> Result<(), Error> {
|
||||
if depth > crate::MAX_NBT_DEPTH {
|
||||
return Err(Error::MaxDepthExceeded);
|
||||
}
|
||||
|
||||
match tag_id {
|
||||
END_ID => Ok(()),
|
||||
BYTE_ID => reader.skip_i8(),
|
||||
@@ -216,7 +228,11 @@ impl NbtTag {
|
||||
if len < 0 {
|
||||
return Err(Error::NegativeLength(len));
|
||||
}
|
||||
reader.skip_bytes(i64::from(len))
|
||||
let len = len as usize;
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
reader.skip_bytes(len as i64)
|
||||
}
|
||||
STRING_ID => reader.skip_string(),
|
||||
LIST_ID => {
|
||||
@@ -225,20 +241,33 @@ impl NbtTag {
|
||||
if len < 0 {
|
||||
return Err(Error::NegativeLength(len));
|
||||
}
|
||||
if tag_type_id == END_ID && len > 0 {
|
||||
return Err(Error::InvalidListTag(tag_type_id));
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
|
||||
for _ in 0..len {
|
||||
Self::skip_data(reader, tag_type_id)?;
|
||||
Self::skip_data_depth(reader, tag_type_id, depth + 1)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
COMPOUND_ID => NbtCompound::skip_content(reader),
|
||||
COMPOUND_ID => NbtCompound::skip_content_depth(reader, depth + 1),
|
||||
INT_ARRAY_ID => {
|
||||
let len = reader.get_i32()?;
|
||||
if len < 0 {
|
||||
return Err(Error::NegativeLength(len));
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
|
||||
for _ in 0..len {
|
||||
reader.skip_i32()?;
|
||||
}
|
||||
@@ -251,6 +280,11 @@ impl NbtTag {
|
||||
return Err(Error::NegativeLength(len));
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
|
||||
for _ in 0..len {
|
||||
reader.skip_i64()?;
|
||||
}
|
||||
@@ -266,6 +300,20 @@ impl NbtTag {
|
||||
reader: &mut R,
|
||||
tag_id: u8,
|
||||
) -> Result<Self, Error> {
|
||||
Self::deserialize_data_depth(reader, tag_id, 0)
|
||||
}
|
||||
|
||||
/// Deserializes a payload whose type is identified by `tag_id` with depth tracking.
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn deserialize_data_depth<'a, R: NbtReadHelper<'a>>(
|
||||
reader: &mut R,
|
||||
tag_id: u8,
|
||||
depth: usize,
|
||||
) -> Result<Self, Error> {
|
||||
if depth > crate::MAX_NBT_DEPTH {
|
||||
return Err(Error::MaxDepthExceeded);
|
||||
}
|
||||
|
||||
match tag_id {
|
||||
END_ID => Ok(Self::End),
|
||||
BYTE_ID => {
|
||||
@@ -299,10 +347,10 @@ impl NbtTag {
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > MAX_ARRAY_LENGTH {
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
let mut byte_array = Vec::with_capacity(len);
|
||||
let mut byte_array = Vec::with_capacity(len.min(4096));
|
||||
for _ in 0..len {
|
||||
let byte = reader.get_i8()?;
|
||||
byte_array.push(byte);
|
||||
@@ -316,22 +364,30 @@ impl NbtTag {
|
||||
if len < 0 {
|
||||
return Err(Error::NegativeLength(len));
|
||||
}
|
||||
if tag_type_id == END_ID && len > 0 {
|
||||
return Err(Error::InvalidListTag(tag_type_id));
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > MAX_ARRAY_LENGTH {
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
|
||||
let mut list = Vec::with_capacity(len);
|
||||
let mut list = Vec::with_capacity(len.min(4096));
|
||||
for _ in 0..len {
|
||||
let tag = Self::deserialize_data(reader, tag_type_id)?;
|
||||
assert_eq!(tag.get_type_id(), tag_type_id);
|
||||
let tag = Self::deserialize_data_depth(reader, tag_type_id, depth + 1)?;
|
||||
if tag.get_type_id() != tag_type_id {
|
||||
return Err(Error::InvalidListTag(tag.get_type_id()));
|
||||
}
|
||||
// Try unwrapping the tag.
|
||||
list.push(Self::flatten(tag));
|
||||
}
|
||||
Ok(Self::List(list))
|
||||
}
|
||||
COMPOUND_ID => Ok(Self::Compound(NbtCompound::deserialize_content(reader)?)),
|
||||
COMPOUND_ID => Ok(Self::Compound(NbtCompound::deserialize_content_depth(
|
||||
reader,
|
||||
depth + 1,
|
||||
)?)),
|
||||
INT_ARRAY_ID => {
|
||||
let len = reader.get_i32()?;
|
||||
if len < 0 {
|
||||
@@ -339,10 +395,11 @@ impl NbtTag {
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > MAX_ARRAY_LENGTH {
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
let mut int_array = Vec::with_capacity(len);
|
||||
|
||||
let mut int_array = Vec::with_capacity(len.min(4096));
|
||||
for _ in 0..len {
|
||||
let int = reader.get_i32()?;
|
||||
int_array.push(int);
|
||||
@@ -356,10 +413,11 @@ impl NbtTag {
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
if len > MAX_ARRAY_LENGTH {
|
||||
if len > crate::MAX_ARRAY_LENGTH {
|
||||
return Err(Error::LargeLength(len));
|
||||
}
|
||||
let mut long_array = Vec::with_capacity(len);
|
||||
|
||||
let mut long_array = Vec::with_capacity(len.min(4096));
|
||||
for _ in 0..len {
|
||||
let long = reader.get_i64()?;
|
||||
long_array.push(long);
|
||||
|
||||
Reference in new issue
Block a user