Skip to content

Commit

Permalink
tendermint: ensure ValidatorIndex is ≤ i32::MAX (#1411)
Browse files Browse the repository at this point in the history
During conversion from usize to ValidatorIndex first convert the value
to `i32` to make sure that the value is ≤ i32::MAX.  Without that
intermediate conversion, `u32::MAX as usize` is successfully converted
into the index.
  • Loading branch information
mina86 committed Apr 17, 2024
1 parent 404349e commit 6399b5b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[tendermint]` Check `index ≤ i32::MAX` invariant when converting `usize`
into `ValidatorIndex`.
([\#1411](https://github.com/informalsystems/tendermint-rs/issues/1411))
14 changes: 11 additions & 3 deletions tendermint/src/vote/validator_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl TryFrom<usize> for ValidatorIndex {
type Error = Error;

fn try_from(value: usize) -> Result<Self, Self::Error> {
Ok(ValidatorIndex(
value.try_into().map_err(Error::integer_overflow)?,
))
// Convert to i32 first to perform ≤ i32::MAX check.
let value = i32::try_from(value).map_err(Error::integer_overflow)?;
ValidatorIndex::try_from(value)
}
}

Expand Down Expand Up @@ -88,3 +88,11 @@ impl FromStr for ValidatorIndex {
)
}
}

#[test]
fn test_i32_max_limit() {
assert!(ValidatorIndex::try_from(u32::MAX).is_err());
assert!(ValidatorIndex::try_from(u32::MAX as usize).is_err());
let value = u32::MAX.to_string();
assert!(ValidatorIndex::from_str(&value).is_err());
}

0 comments on commit 6399b5b

Please sign in to comment.