Merge pull request #221 from gamedig/fix/read-string-may-panic-index-out-of-range

This commit is contained in:
Cain 2024-09-07 11:55:30 +01:00 committed by GitHub
commit ada3c548f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 ..];