Skip to content

Commit

Permalink
Merge pull request #221 from gamedig/fix/read-string-may-panic-index-…
Browse files Browse the repository at this point in the history
…out-of-range
  • Loading branch information
cainthebest authored Sep 7, 2024
2 parents 7057121 + 4784e0a commit ada3c54
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/lib/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: StringDecoder>(&mut self, until: Option<D::Delimiter>) -> GDResult<String> {
// Check if the cursor is out of bounds.
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()
)));
}

// Slice the data array from the current cursor position to the end.
let data_slice = &self.data[self.cursor ..];

Expand Down

0 comments on commit ada3c54

Please sign in to comment.