Merge pull request #7 from laanwj/2015_10_plug_leak

Fix data leak in CommandString::consensus_encode
This commit is contained in:
Andrew Poelstra 2015-11-07 21:04:42 -06:00
commit f9fc368ad9
1 changed files with 7 additions and 6 deletions

View File

@ -40,14 +40,15 @@ pub struct CommandString(pub String);
impl<S: SimpleEncoder> ConsensusEncodable<S> for CommandString { impl<S: SimpleEncoder> ConsensusEncodable<S> for CommandString {
#[inline] #[inline]
fn consensus_encode(&self, s: &mut S) -> Result<(), S::Error> { fn consensus_encode(&self, s: &mut S) -> Result<(), S::Error> {
use std::intrinsics::copy_nonoverlapping;
use std::mem;
let &CommandString(ref inner_str) = self; let &CommandString(ref inner_str) = self;
let mut rawbytes = [0u8; 12]; let mut rawbytes = [0u8; 12];
unsafe { copy_nonoverlapping(inner_str.as_bytes().as_ptr(), let strbytes = inner_str.as_bytes();
rawbytes.as_mut_ptr(), if strbytes.len() > 12 {
mem::size_of::<[u8; 12]>()); } panic!("Command string longer than 12 bytes");
}
for x in 0..strbytes.len() {
rawbytes[x] = strbytes[x];
}
rawbytes.consensus_encode(s) rawbytes.consensus_encode(s)
} }
} }