fix(clippy): new lints in nightly Rust (#3541)

* fix(clippy): for loop with only one item

* fix(clippy): manual Range::contains

Also clarified the surrounding code because it was unclear.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
teor 2022-02-16 02:31:36 +10:00 committed by GitHub
parent 08828c1b6a
commit 74ad1825cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 23 deletions

View File

@ -174,14 +174,13 @@ fn checkpoint_list_duplicate_heights_fail() -> Result<(), BoxError> {
// Parse the genesis block // Parse the genesis block
let mut checkpoint_data = Vec::new(); let mut checkpoint_data = Vec::new();
for b in &[&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..]] { let block =
let block = Arc::<Block>::zcash_deserialize(*b)?; Arc::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..])?;
let hash = block.hash(); let hash = block.hash();
checkpoint_data.push(( checkpoint_data.push((
block.coinbase_height().expect("test block has height"), block.coinbase_height().expect("test block has height"),
hash, hash,
)); ));
}
// Then add some fake entries with duplicate heights // Then add some fake entries with duplicate heights
checkpoint_data.push((block::Height(1), block::Hash([0xaa; 32]))); checkpoint_data.push((block::Height(1), block::Hash([0xaa; 32])));
@ -202,14 +201,13 @@ fn checkpoint_list_duplicate_hashes_fail() -> Result<(), BoxError> {
// Parse the genesis block // Parse the genesis block
let mut checkpoint_data = Vec::new(); let mut checkpoint_data = Vec::new();
for b in &[&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..]] { let block =
let block = Arc::<Block>::zcash_deserialize(*b)?; Arc::<Block>::zcash_deserialize(&zebra_test::vectors::BLOCK_MAINNET_GENESIS_BYTES[..])?;
let hash = block.hash(); let hash = block.hash();
checkpoint_data.push(( checkpoint_data.push((
block.coinbase_height().expect("test block has height"), block.coinbase_height().expect("test block has height"),
hash, hash,
)); ));
}
// Then add some fake entries with duplicate hashes // Then add some fake entries with duplicate hashes
checkpoint_data.push((block::Height(1), block::Hash([0xcc; 32]))); checkpoint_data.push((block::Height(1), block::Hash([0xcc; 32])));

View File

@ -633,17 +633,23 @@ impl Codec {
} }
fn read_filterload<R: Read>(&self, mut reader: R, body_len: usize) -> Result<Message, Error> { fn read_filterload<R: Read>(&self, mut reader: R, body_len: usize) -> Result<Message, Error> {
const MAX_FILTERLOAD_LENGTH: usize = 36000; // The maximum length of a filter.
const FILTERLOAD_REMAINDER_LENGTH: usize = 4 + 4 + 1; const MAX_FILTERLOAD_FILTER_LENGTH: usize = 36000;
if !(FILTERLOAD_REMAINDER_LENGTH <= body_len // The data length of the fields:
&& body_len <= FILTERLOAD_REMAINDER_LENGTH + MAX_FILTERLOAD_LENGTH) // hash_functions_count + tweak + flags.
{ const FILTERLOAD_FIELDS_LENGTH: usize = 4 + 4 + 1;
// The maximum length of a filter message's data.
const MAX_FILTERLOAD_MESSAGE_LENGTH: usize =
MAX_FILTERLOAD_FILTER_LENGTH + FILTERLOAD_FIELDS_LENGTH;
if !(FILTERLOAD_FIELDS_LENGTH..=MAX_FILTERLOAD_MESSAGE_LENGTH).contains(&body_len) {
return Err(Error::Parse("Invalid filterload message body length.")); return Err(Error::Parse("Invalid filterload message body length."));
} }
// Memory Denial of Service: we just limited the untrusted parsed length // Memory Denial of Service: we just checked the untrusted parsed length
let filter_length: usize = body_len - FILTERLOAD_REMAINDER_LENGTH; let filter_length: usize = body_len - FILTERLOAD_FIELDS_LENGTH;
let filter_bytes = zcash_deserialize_bytes_external_count(filter_length, &mut reader)?; let filter_bytes = zcash_deserialize_bytes_external_count(filter_length, &mut reader)?;
Ok(Message::FilterLoad { Ok(Message::FilterLoad {