Skip to content

Commit

Permalink
Sanity-check vector length when deserializing
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Apr 17, 2017
1 parent 4b73c32 commit cdb452f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[package]
name = "bitcoin"
version = "0.9.0"
version = "0.9.1"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-bitcoin/"
Expand Down
9 changes: 8 additions & 1 deletion src/network/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@

use std::collections::HashMap;
use std::hash::Hash;
use std::u32;
use std::{mem, u32};

use util::hash::Sha256dHash;
use network::serialize::{SimpleDecoder, SimpleEncoder};

/// Maximum size, in bytes, of a vector we are allowed to decode
pub const MAX_VEC_SIZE: usize = 32 * 1024 * 1024;

/// Data which can be encoded in a consensus-consistent way
pub trait ConsensusEncodable<S: SimpleEncoder> {
/// Encode an object with a well-defined format
Expand Down Expand Up @@ -185,6 +188,10 @@ impl<D: SimpleDecoder, T: ConsensusDecodable<D>> ConsensusDecodable<D> for Vec<T
#[inline]
fn consensus_decode(d: &mut D) -> Result<Vec<T>, D::Error> {
let VarInt(len): VarInt = try!(ConsensusDecodable::consensus_decode(d));
let byte_size = len as usize * mem::size_of::<T>();
if byte_size > MAX_VEC_SIZE {
return Err(d.error(format!("tried to allocate vec of size {} (max {})", byte_size, MAX_VEC_SIZE)));
}
let mut ret = Vec::with_capacity(len as usize);
for _ in 0..len { ret.push(try!(ConsensusDecodable::consensus_decode(d))); }
Ok(ret)
Expand Down

0 comments on commit cdb452f

Please sign in to comment.