Skip to content

Commit

Permalink
Add associated method to DecodeLimit for advancing input index (#286)
Browse files Browse the repository at this point in the history
* Add associated method to DecodeLimit for advancing input index

* Run cargo update

* remove flacky test false positive

* Disable another flaky test

* Disable yet another flaky test

* Bump version to 2.3.0

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
  • Loading branch information
KiChjang and gui1117 authored Sep 11, 2021
1 parent 6213b06 commit 205bb1a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this crate are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this crate adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.3.0] - 2021-09-11

### Added
- `decode_and_advance_with_depth_limit` to the `DecodeLimit` trait. This allows advancing the cursor while decoding the input. PR #286

## [2.2.0] - 2021-07-02

### Added
Expand Down
80 changes: 40 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "parity-scale-codec"
description = "SCALE - Simple Concatenating Aggregated Little Endians"
version = "2.2.0"
version = "2.3.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"
repository = "https://github.com/paritytech/parity-scale-codec"
Expand Down
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "parity-scale-codec-derive"
description = "Serialization and deserialization derive macro for Parity SCALE Codec"
version = "2.2.0"
version = "2.3.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"
edition = "2018"
Expand Down
14 changes: 14 additions & 0 deletions src/bit_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ mod tests {
}

#[test]
// Flaky test due to:
// * https://github.com/bitvecto-rs/bitvec/issues/135
// * https://github.com/rust-lang/miri/issues/1866
#[cfg(not(miri))]

fn bitvec_u16() {
for v in &test_data!(u16) {
let encoded = v.encode();
Expand All @@ -194,6 +199,11 @@ mod tests {
}

#[test]
// Flaky test due to:
// * https://github.com/bitvecto-rs/bitvec/issues/135
// * https://github.com/rust-lang/miri/issues/1866
#[cfg(not(miri))]

fn bitvec_u64() {
for v in &test_data!(u64) {
let encoded = v.encode();
Expand All @@ -211,6 +221,10 @@ mod tests {
}

#[test]
// Flaky test due to:
// * https://github.com/bitvecto-rs/bitvec/issues/135
// * https://github.com/rust-lang/miri/issues/1866
#[cfg(not(miri))]
fn bitbox() {
let data: &[u8] = &[5, 10];
let slice = BitSlice::<Msb0, u8>::from_slice(data).unwrap();
Expand Down
25 changes: 25 additions & 0 deletions src/depth_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub trait DecodeLimit: Sized {
/// If `limit` is hit, an error is returned.
fn decode_with_depth_limit(limit: u32, input: &[u8]) -> Result<Self, Error>;

/// Decode `Self` and advance `input` by the number of bytes consumed.
///
/// If `limit` is hit, an error is returned.
fn decode_and_advance_with_depth_limit<I: Input>(limit: u32, input: &mut I) -> Result<Self, Error>;

/// Decode `Self` and consume all of the given input data.
///
/// If not all data is consumed or `limit` is hit, an error is returned.
Expand Down Expand Up @@ -82,6 +87,15 @@ impl<T: Decode> DecodeLimit for T {
}
}

fn decode_and_advance_with_depth_limit<I: Input>(limit: u32, input: &mut I) -> Result<Self, Error> {
let mut input = DepthTrackingInput {
input,
depth: 0,
max_depth: limit,
};
T::decode(&mut input)
}

fn decode_with_depth_limit(limit: u32, input: &[u8]) -> Result<Self, Error> {
let mut input = DepthTrackingInput {
input: &mut &input[..],
Expand All @@ -107,4 +121,15 @@ mod tests {
assert_eq!(decoded, nested);
assert!(NestedVec::decode_with_depth_limit(2, &encoded).is_err());
}

#[test]
fn decode_and_advance_works() {
type NestedVec = Vec<Vec<Vec<Vec<u8>>>>;
let nested: NestedVec = vec![vec![vec![vec![1]]]];
let encoded = &mut &nested.encode()[..];

let decoded = Vec::<u8>::decode_and_advance_with_depth_limit(1, encoded).unwrap();
assert_eq!(decoded, vec![4]);
assert!(NestedVec::decode_with_depth_limit(3, encoded).is_err());
}
}

0 comments on commit 205bb1a

Please sign in to comment.