From 2a9eff409e3e27736dbba896a619162a92520889 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Mon, 16 Sep 2024 21:29:15 +0200 Subject: [PATCH] Minior bytebuf improvements --- pumpkin-protocol/src/bytebuf/mod.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pumpkin-protocol/src/bytebuf/mod.rs b/pumpkin-protocol/src/bytebuf/mod.rs index 4ec1dc5a6..3b46f2f90 100644 --- a/pumpkin-protocol/src/bytebuf/mod.rs +++ b/pumpkin-protocol/src/bytebuf/mod.rs @@ -72,19 +72,19 @@ impl ByteBuffer { } pub fn get_string(&mut self) -> Result { - self.get_string_len(32767) + self.get_string_len(i16::MAX as i32) } - pub fn get_string_len(&mut self, max_size: usize) -> Result { + pub fn get_string_len(&mut self, max_size: i32) -> Result { let size = self.get_var_int()?.0; - if size as usize > max_size { + if size > max_size { return Err(DeserializerError::Message( "String length is bigger than max size".to_string(), )); } let data = self.copy_to_bytes(size as usize)?; - if data.len() > max_size { + if data.len() as i32 > max_size { return Err(DeserializerError::Message( "String is bigger than max size".to_string(), )); @@ -125,6 +125,14 @@ impl ByteBuffer { } pub fn put_string(&mut self, val: &str) { + self.put_string_len(val, i16::MAX as i32); + } + + pub fn put_string_len(&mut self, val: &str, max_size: i32) { + if val.len() as i32 > max_size { + // Should be panic?, I mean its our fault + panic!("String is too big"); + } self.put_var_int(&val.len().into()); self.buffer.put(val.as_bytes()); }