fix panics

This commit is contained in:
Ubuntu 2022-08-31 18:48:51 +00:00
parent 0085f8a565
commit efb1f3f2d5
2 changed files with 14 additions and 23 deletions

View file

@ -1,8 +1,6 @@
use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable};
use azalea_buf::McBuf;
use azalea_core::ResourceLocation;
use packet_macros::ClientboundGamePacket;
use std::io::{Read, Write};
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundRecipePacket {
@ -27,27 +25,9 @@ pub struct RecipeBookSettings {
pub smoker_filtering_craftable: bool,
}
#[derive(Clone, Debug, Copy)]
#[derive(Clone, Debug, Copy, McBuf)]
pub enum State {
Init = 0,
Add = 1,
Remove = 2,
}
impl McBufWritable for State {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
buf.write_varint(*self as i32)?;
Ok(())
}
}
impl McBufReadable for State {
fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let state = buf.read_varint()?;
Ok(match state {
0 => State::Init,
1 => State::Add,
2 => State::Remove,
_ => panic!("Invalid state: {}", state),
})
}
}

View file

@ -50,6 +50,8 @@ pub enum FrameSplitterError {
#[from]
source: std::io::Error,
},
#[error("Packet is longer than {max} bytes (is {size})")]
BadLength { max: u32, size: u32 },
}
async fn frame_splitter<R: ?Sized>(mut stream: &mut R) -> Result<Vec<u8>, FrameSplitterError>
@ -57,7 +59,16 @@ where
R: AsyncRead + std::marker::Unpin + std::marker::Send,
{
// Packet Length
let length = read_varint_async(&mut stream).await?;
let length = read_varint_async(&mut stream).await? as u32;
let max_length: u32 = 2u32.pow(20u32); // 1mb, arbitrary
if length > max_length {
// minecraft *probably* won't send packets bigger than this
return Err(FrameSplitterError::BadLength {
max: max_length,
size: length,
});
}
let mut buf = vec![0; length as usize];
stream.read_exact(&mut buf).await?;