From 73f39510fe992b64d675540a4dd3b33ede47b66f Mon Sep 17 00:00:00 2001 From: Cain <75994858+cainthebest@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:23:57 +0100 Subject: [PATCH 1/4] fix(buf): add error for out of bounds panic on read string --- crates/lib/src/buffer.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/lib/src/buffer.rs b/crates/lib/src/buffer.rs index 967ed4a0..5485d901 100644 --- a/crates/lib/src/buffer.rs +++ b/crates/lib/src/buffer.rs @@ -145,6 +145,15 @@ impl<'a, B: ByteOrder> Buffer<'a, B> { /// /// Returns a `BufferError` if there is an error decoding the string. pub fn read_string(&mut self, until: Option) -> GDResult { + // Check if the cursor is out of bounds. + let remaining = self.remaining_length(); + if self.cursor > remaining { + return Err(PacketUnderflow.context(format!( + "Cursor position {} is out of bounds when reading string. Remaining bytes: {remaining}", + self.cursor + ))); + } + // Slice the data array from the current cursor position to the end. let data_slice = &self.data[self.cursor ..]; From e39b8803643a31f62efc3512bc7d4603299752b3 Mon Sep 17 00:00:00 2001 From: Cain <75994858+cainthebest@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:33:53 +0100 Subject: [PATCH 2/4] refactor(buf): clean up out of bounds err msg --- crates/lib/src/buffer.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/lib/src/buffer.rs b/crates/lib/src/buffer.rs index 5485d901..549fabf5 100644 --- a/crates/lib/src/buffer.rs +++ b/crates/lib/src/buffer.rs @@ -146,11 +146,10 @@ impl<'a, B: ByteOrder> Buffer<'a, B> { /// Returns a `BufferError` if there is an error decoding the string. pub fn read_string(&mut self, until: Option) -> GDResult { // Check if the cursor is out of bounds. - let remaining = self.remaining_length(); - if self.cursor > remaining { + if self.cursor > self.remaining_length() { return Err(PacketUnderflow.context(format!( - "Cursor position {} is out of bounds when reading string. Remaining bytes: {remaining}", - self.cursor + "Cursor position {} is out of bounds when reading string. Buffer length: {}", + self.cursor, self.data.len() ))); } From 786e9dad9487eafd1319b70a628b8d917b45821c Mon Sep 17 00:00:00 2001 From: Cain <75994858+cainthebest@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:53:21 +0100 Subject: [PATCH 3/4] fix(buf): use correct len fn on OOB check --- crates/lib/src/buffer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/lib/src/buffer.rs b/crates/lib/src/buffer.rs index 549fabf5..581ce8f2 100644 --- a/crates/lib/src/buffer.rs +++ b/crates/lib/src/buffer.rs @@ -146,10 +146,10 @@ impl<'a, B: ByteOrder> Buffer<'a, B> { /// Returns a `BufferError` if there is an error decoding the string. pub fn read_string(&mut self, until: Option) -> GDResult { // Check if the cursor is out of bounds. - if self.cursor > self.remaining_length() { + if self.cursor > self.data_length() { return Err(PacketUnderflow.context(format!( "Cursor position {} is out of bounds when reading string. Buffer length: {}", - self.cursor, self.data.len() + self.cursor, self.data_length() ))); } From 4784e0a2812085f4f7d347418cdd5f5d37dd1581 Mon Sep 17 00:00:00 2001 From: Cain <75994858+cainthebest@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:58:27 +0100 Subject: [PATCH 4/4] chore(buf): format a line for ci --- crates/lib/src/buffer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/lib/src/buffer.rs b/crates/lib/src/buffer.rs index 581ce8f2..d16745d4 100644 --- a/crates/lib/src/buffer.rs +++ b/crates/lib/src/buffer.rs @@ -149,7 +149,8 @@ impl<'a, B: ByteOrder> Buffer<'a, B> { if self.cursor > self.data_length() { return Err(PacketUnderflow.context(format!( "Cursor position {} is out of bounds when reading string. Buffer length: {}", - self.cursor, self.data_length() + self.cursor, + self.data_length() ))); }