From 85785c9ebeb162d868a49b50b141dd251df6ca4a Mon Sep 17 00:00:00 2001 From: lukas0008 Date: Tue, 13 Aug 2024 14:53:55 +0200 Subject: [PATCH] Fix: Server panics when loading certain chunks Cause: The size that is given in the file for the length of the chunk includes the compression byte, thus when reading a chunk that is at the end of the file, it would read 1 byte more that should be possible, causing a panic. --- pumpkin-world/src/world.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pumpkin-world/src/world.rs b/pumpkin-world/src/world.rs index 6f705c171..f297ccdfd 100644 --- a/pumpkin-world/src/world.rs +++ b/pumpkin-world/src/world.rs @@ -81,9 +81,10 @@ impl Level { .into_iter() .map(|(region, chunk_vec)| { let mut path = self.root_folder.clone(); + let chunk_vec = chunk_vec.collect_vec(); path.push("region"); path.push(format!("r.{}.{}.mca", region.0, region.1)); - self.read_region_chunks(path, chunk_vec.collect_vec()) + self.read_region_chunks(path, chunk_vec) }), ) .await @@ -93,12 +94,12 @@ impl Level { } async fn read_region_chunks( &self, - region_file: PathBuf, + region_file_path: PathBuf, chunks: Vec<(i32, i32)>, ) -> Vec<((i32, i32), Result)> { // dbg!(at); // return different error when file is not found (because that means that the chunks have just not been generated yet) - let mut region_file = match File::open(region_file).await { + let mut region_file = match File::open(®ion_file_path).await { Ok(f) => f, Err(err) => match err.kind() { std::io::ErrorKind::NotFound => { @@ -190,12 +191,13 @@ impl Level { match compression { Compression::Zlib => {} - _ => panic!(), // TODO: support other compression types + _ => panic!("Compression type is not supported"), // TODO: support other compression types } let size = u32::from_be_bytes(header[0..4].try_into().unwrap()); - - let chunk_data = file_buf.drain(0..size as usize).collect_vec(); + + // size includes the compression scheme byte, so we need to subtract 1 + let chunk_data = file_buf.drain(0..size as usize - 1).collect_vec(); ((old_chunk_x, old_chunk_z), Ok(chunk_data)) }