Simplify types used in AddrV2 size check (#6368)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
teor 2023-03-22 14:48:53 +10:00 committed by GitHub
parent 474182a355
commit ff54f86296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 8 deletions

View File

@ -6,7 +6,6 @@
//! Zebra never sends `addrv2` messages, because peers still accept `addr` (v1) messages. //! Zebra never sends `addrv2` messages, because peers still accept `addr` (v1) messages.
use std::{ use std::{
convert::{TryFrom, TryInto},
io::Read, io::Read,
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
}; };
@ -15,8 +14,8 @@ use byteorder::{BigEndian, ReadBytesExt};
use thiserror::Error; use thiserror::Error;
use zebra_chain::serialization::{ use zebra_chain::serialization::{
zcash_deserialize_bytes_external_count, CompactSize64, DateTime32, SerializationError, zcash_deserialize_bytes_external_count, CompactSize64, CompactSizeMessage, DateTime32,
TrustedPreallocate, ZcashDeserialize, ZcashDeserializeInto, SerializationError, TrustedPreallocate, ZcashDeserialize, ZcashDeserializeInto,
}; };
use crate::{ use crate::{
@ -283,17 +282,16 @@ impl ZcashDeserialize for AddrV2 {
let network_id = reader.read_u8()?; let network_id = reader.read_u8()?;
// > CompactSize The length in bytes of addr. // > CompactSize The length in bytes of addr.
let max_size = MAX_ADDR_V2_ADDR_SIZE as u64; // `MAX_ADDR_V2_ADDR_SIZE` fits in `u64`. let addr_len: CompactSizeMessage = (&mut reader).zcash_deserialize_into()?;
let addr_len: CompactSize64 = (&mut reader).zcash_deserialize_into()?; let addr_len: usize = addr_len.into();
if addr_len > CompactSize64::from(max_size) { if addr_len > MAX_ADDR_V2_ADDR_SIZE {
return Err(SerializationError::Parse( return Err(SerializationError::Parse(
"addr field longer than MAX_ADDR_V2_ADDR_SIZE in addrv2 message", "addr field longer than MAX_ADDR_V2_ADDR_SIZE in addrv2 message",
)); ));
} }
// > uint8[sizeAddr] Network address. The interpretation depends on networkID. // > uint8[sizeAddr] Network address. The interpretation depends on networkID.
let addr: Vec<u8> = let addr: Vec<u8> = zcash_deserialize_bytes_external_count(addr_len, &mut reader)?;
zcash_deserialize_bytes_external_count(u64::from(addr_len) as usize, &mut reader)?;
// > uint16 Network port. If not relevant for the network this MUST be 0. // > uint16 Network port. If not relevant for the network this MUST be 0.
let port = reader.read_u16::<BigEndian>()?; let port = reader.read_u16::<BigEndian>()?;