diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-10-07 20:12:36 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-07 20:12:36 -0500 |
| commit | bc3aa9467ae1e2d0ea1727093af9b0af14965e69 (patch) | |
| tree | 8db3b735daed484507129eb0683db88ddec14210 /azalea-core/src | |
| parent | 695efef66fdf1e08f0cb6d8783c085875100fa2d (diff) | |
| download | azalea-drasl-bc3aa9467ae1e2d0ea1727093af9b0af14965e69.tar.xz | |
Replace impl Read with Cursor<&[u8]> (#26)
* Start getting rid of Cursor
* try to make the tests pass and fail
* make the tests pass
* remove unused uses
* fix clippy warnings
* fix potential OOM exploits
* fix OOM in az-nbt
* fix nbt benchmark
* fix a test
* start replacing it with Cursor<Vec<u8>>
* wip
* fix all the issues
* fix all tests
* fix nbt benchmark
* fix warnings
Diffstat (limited to 'azalea-core/src')
| -rwxr-xr-x | azalea-core/src/difficulty.rs | 4 | ||||
| -rwxr-xr-x | azalea-core/src/game_type.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/particle/mod.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/position.rs | 16 | ||||
| -rwxr-xr-x | azalea-core/src/resource_location.rs | 8 | ||||
| -rw-r--r-- | azalea-core/src/slot.rs | 4 |
6 files changed, 20 insertions, 24 deletions
diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs index 65950e8d..5cc2f9fa 100755 --- a/azalea-core/src/difficulty.rs +++ b/azalea-core/src/difficulty.rs @@ -1,6 +1,6 @@ use std::{ fmt::{Debug, Error, Formatter}, - io::{Read, Write}, + io::{Cursor, Write}, }; use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; @@ -67,7 +67,7 @@ impl Difficulty { } impl McBufReadable for Difficulty { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { Ok(Difficulty::by_id(u8::read_from(buf)?)) } } diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index f3f056a4..635ee6fe 100755 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,5 +1,5 @@ use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; -use std::io::{Read, Write}; +use std::io::{Cursor, Write}; #[derive(Hash, Copy, Clone, Debug)] pub enum GameType { @@ -79,7 +79,7 @@ impl GameType { } impl McBufReadable for GameType { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let id = u8::read_from(buf)?; GameType::from_id(id).ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 }) } @@ -108,7 +108,7 @@ impl From<OptionalGameType> for Option<GameType> { } impl McBufReadable for OptionalGameType { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let id = i8::read_from(buf)?; GameType::from_optional_id(id).ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 }) } diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index c2fa3337..8087de51 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -1,6 +1,6 @@ use crate::{BlockPos, Slot}; use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufWritable}; -use std::io::{Read, Write}; +use std::io::{Cursor, Write}; #[derive(Debug, Clone, McBuf)] pub struct Particle { @@ -153,7 +153,7 @@ pub struct VibrationParticle { } impl ParticleData { - pub fn read_from_particle_id(buf: &mut impl Read, id: u32) -> Result<Self, BufReadError> { + pub fn read_from_particle_id(buf: &mut Cursor<&[u8]>, id: u32) -> Result<Self, BufReadError> { Ok(match id { 0 => ParticleData::AmbientEntityEffect, 1 => ParticleData::AngryVillager, @@ -249,7 +249,7 @@ impl ParticleData { } impl McBufReadable for ParticleData { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let id = u32::var_read_from(buf)?; ParticleData::read_from_particle_id(buf, id) } diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index d47072d4..d408d817 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -1,7 +1,7 @@ use crate::ResourceLocation; use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; use std::{ - io::{Read, Write}, + io::{Cursor, Write}, ops::{Add, Mul, Rem}, }; @@ -301,7 +301,7 @@ const Z_OFFSET: u64 = PACKED_Y_LENGTH; const X_OFFSET: u64 = PACKED_Y_LENGTH + PACKED_Z_LENGTH; impl McBufReadable for BlockPos { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let val = i64::read_from(buf)?; let x = (val << (64 - X_OFFSET - PACKED_X_LENGTH) >> (64 - PACKED_X_LENGTH)) as i32; let y = (val << (64 - PACKED_Y_LENGTH) >> (64 - PACKED_Y_LENGTH)) as i32; @@ -311,7 +311,7 @@ impl McBufReadable for BlockPos { } impl McBufReadable for GlobalPos { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { Ok(GlobalPos { dimension: ResourceLocation::read_from(buf)?, pos: BlockPos::read_from(buf)?, @@ -320,7 +320,7 @@ impl McBufReadable for GlobalPos { } impl McBufReadable for ChunkSectionPos { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let long = i64::read_from(buf)?; Ok(ChunkSectionPos { x: (long >> 42) as i32, @@ -361,8 +361,6 @@ impl McBufWritable for ChunkSectionPos { #[cfg(test)] mod tests { - use std::io::Cursor; - use super::*; #[test] @@ -403,10 +401,10 @@ mod tests { #[test] fn test_read_blockpos_from() { - let mut buf = Cursor::new(Vec::new()); + let mut buf = Vec::new(); 13743895338965u64.write_into(&mut buf).unwrap(); - buf.set_position(0); - let block_pos = BlockPos::read_from(&mut buf).unwrap(); + let buf = &mut &buf[..]; + let block_pos = BlockPos::read_from(buf).unwrap(); assert_eq!(block_pos, BlockPos::new(49, -43, -3)); } } diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 4c0764e5..5f8dcea1 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -1,7 +1,7 @@ //! A resource, like minecraft:stone use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; -use std::io::{Read, Write}; +use std::io::{Cursor, Write}; #[derive(Hash, Clone, PartialEq, Eq)] pub struct ResourceLocation { @@ -46,7 +46,7 @@ impl std::fmt::Debug for ResourceLocation { } impl McBufReadable for ResourceLocation { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let location_string = String::read_from(buf)?; ResourceLocation::new(&location_string) } @@ -59,8 +59,6 @@ impl McBufWritable for ResourceLocation { #[cfg(test)] mod tests { - use std::io::Cursor; - use super::*; #[test] @@ -96,7 +94,7 @@ mod tests { .write_into(&mut buf) .unwrap(); - let mut buf = Cursor::new(buf); + let mut buf = &mut &buf[..]; assert_eq!( ResourceLocation::read_from(&mut buf).unwrap(), diff --git a/azalea-core/src/slot.rs b/azalea-core/src/slot.rs index 38abcf61..d60c7390 100644 --- a/azalea-core/src/slot.rs +++ b/azalea-core/src/slot.rs @@ -1,7 +1,7 @@ // TODO: have an azalea-inventory or azalea-container crate and put this there use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; -use std::io::{Read, Write}; +use std::io::{Cursor, Write}; #[derive(Debug, Clone)] pub enum Slot { @@ -18,7 +18,7 @@ pub struct SlotData { } impl McBufReadable for Slot { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { + fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let present = bool::read_from(buf)?; if !present { return Ok(Slot::Empty); |
