Skip to content

Commit

Permalink
bit-sequence: Checked decode
Browse files Browse the repository at this point in the history
Decode the bytes using checked access. This ensures we don't panic on
missing input.
  • Loading branch information
bkchr committed May 7, 2024
1 parent 34d45a7 commit 76be3c8
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions scale-decode/src/visitor/types/bit_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ impl<'scale> BitSequence<'scale> {
Ok(bytes_after)
} else {
let decoder = decode_using_format_from(self.bytes, self.format)?;
Ok(&self.bytes[decoder.encoded_size()..])
self.bytes.get(decoder.encoded_size()..).ok_or_else(|| DecodeError::NotEnoughInput)
}
}

/// Return a decoder to decode the bits in this bit sequence.
pub fn decode(&mut self) -> Result<Decoder<'scale>, DecodeError> {
let decoder = decode_using_format_from(self.bytes, self.format)?;
self.bytes_after = Some(&self.bytes[decoder.encoded_size()..]);
self.bytes_after = Some(
&self.bytes.get(decoder.encoded_size()..).ok_or_else(|| DecodeError::NotEnoughInput)?,
);
Ok(decoder)
}
}
Expand Down

0 comments on commit 76be3c8

Please sign in to comment.