Skip to content

Commit

Permalink
Restructure code to reduce asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed Sep 26, 2016
1 parent 2a002bb commit 7af1e3e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/msgs/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,13 @@ impl Codec for Random {

impl Random {
pub fn from_slice(bytes: &[u8]) -> Random {
assert_eq!(bytes.len(), 32);
let mut rd = Reader::init(&bytes);
Random::read(&mut rd).unwrap()
}

pub fn write_slice(&self, mut bytes: &mut [u8]) {
let mut buf = Vec::new();
self.encode(&mut buf);
assert_eq!(bytes.write(&buf).unwrap(), 32);
let buf = self.get_encoding();
bytes.write(&buf).unwrap();
}
}

Expand All @@ -85,7 +83,7 @@ pub struct SessionID {

impl Codec for SessionID {
fn encode(&self, bytes: &mut Vec<u8>) {
assert!(self.bytes.len() <= 0xff);
debug_assert!(self.bytes.len() <= 32);
bytes.push(self.bytes.len() as u8);
bytes.extend_from_slice(&self.bytes);
}
Expand All @@ -103,7 +101,8 @@ impl Codec for SessionID {
}

impl SessionID {
pub fn new(bytes: Vec<u8>) -> SessionID {
pub fn new(mut bytes: Vec<u8>) -> SessionID {
bytes.truncate(32);
SessionID { bytes: bytes }
}

Expand Down
3 changes: 1 addition & 2 deletions src/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ fn wrap_in_asn1_len(bytes: &mut Vec<u8>) {
} else if len < 256 {
bytes.insert(0, 0x81u8);
bytes.insert(1, len as u8);
} else {
assert!(len <= 0xffff);
} else if len < 0xffff {
bytes.insert(0, 0x82u8);
bytes.insert(1, ((len >> 8) & 0xff) as u8);
bytes.insert(2, (len & 0xff) as u8);
Expand Down

0 comments on commit 7af1e3e

Please sign in to comment.