From 63b1036a96c45b0fefc6ca2501f1cc479acc95de Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 21 Feb 2025 17:22:00 +0000 Subject: fix CustomModelData and WrittenBookContent datacomponents --- azalea-buf/src/read.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'azalea-buf/src') diff --git a/azalea-buf/src/read.rs b/azalea-buf/src/read.rs index d7a55716..71c96c34 100755 --- a/azalea-buf/src/read.rs +++ b/azalea-buf/src/read.rs @@ -21,6 +21,8 @@ pub enum BufReadError { CouldNotReadBytes, #[error("The received encoded string buffer length is longer than maximum allowed ({length} > {max_length})")] StringLengthTooLong { length: u32, max_length: u32 }, + #[error("The received Vec length is longer than maximum allowed ({length} > {max_length})")] + VecLengthTooLong { length: u32, max_length: u32 }, #[error("{source}")] Io { #[from] @@ -183,7 +185,7 @@ impl AzaleaRead for UnsizedByteArray { } } -impl AzaleaRead for Vec { +impl AzaleaRead for Vec { default fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { let length = u32::azalea_read_var(buf)? as usize; // we limit the capacity to not get exploited into allocating a bunch @@ -194,6 +196,23 @@ impl AzaleaRead for Vec { Ok(contents) } } +impl AzaleaReadLimited for Vec { + fn azalea_read_limited(buf: &mut Cursor<&[u8]>, limit: usize) -> Result { + let length = u32::azalea_read_var(buf)? as usize; + if length > limit { + return Err(BufReadError::VecLengthTooLong { + length: length as u32, + max_length: limit as u32, + }); + } + + let mut contents = Vec::with_capacity(usize::min(length, 65536)); + for _ in 0..length { + contents.push(T::azalea_read(buf)?); + } + Ok(contents) + } +} impl AzaleaRead for HashMap { fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result { @@ -343,6 +362,16 @@ impl AzaleaReadVar for Option { }) } } +impl AzaleaReadLimited for Option { + fn azalea_read_limited(buf: &mut Cursor<&[u8]>, limit: usize) -> Result { + let present = bool::azalea_read(buf)?; + Ok(if present { + Some(T::azalea_read_limited(buf, limit)?) + } else { + None + }) + } +} // [String; 4] impl AzaleaRead for [T; N] { -- cgit v1.2.3