diff --git a/Cargo.toml b/Cargo.toml index 19f3ebdde..c7f8c458d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,7 +85,7 @@ flate2 = { version="1.0", optional = true, default-features = false } mio = "^0.6.13" net2 = "0.2" bytes = "0.4" -byteorder = "1" +byteorder = "1.2" futures = "0.1" futures-cpupool = "0.1" slab = "0.4" diff --git a/src/extensions.rs b/src/extensions.rs index 024873257..da7b5ba24 100644 --- a/src/extensions.rs +++ b/src/extensions.rs @@ -1,7 +1,7 @@ use std::any::{Any, TypeId}; use std::collections::HashMap; -use std::hash::{BuildHasherDefault, Hasher}; use std::fmt; +use std::hash::{BuildHasherDefault, Hasher}; struct IdHasher { id: u64, @@ -16,7 +16,7 @@ impl Default for IdHasher { impl Hasher for IdHasher { fn write(&mut self, bytes: &[u8]) { for &x in bytes { - self.id.wrapping_add(x as u64); + self.id.wrapping_add(u64::from(x)); } } diff --git a/src/ws/frame.rs b/src/ws/frame.rs index 5e88758dd..871cc7619 100644 --- a/src/ws/frame.rs +++ b/src/ws/frame.rs @@ -1,9 +1,8 @@ -#![cfg_attr(feature = "cargo-clippy", allow(cast_ptr_alignment))] -use byteorder::{ByteOrder, NetworkEndian}; +use byteorder::{ByteOrder, LittleEndian, NetworkEndian}; use bytes::{BufMut, Bytes, BytesMut}; use futures::{Async, Poll, Stream}; use rand; -use std::{fmt, ptr}; +use std::fmt; use body::Binary; use error::PayloadError; @@ -115,8 +114,7 @@ impl Frame { }; let mask: &[u8] = &buf[idx..idx + 4]; - let mask_u32: u32 = - unsafe { ptr::read_unaligned(mask.as_ptr() as *const u32) }; + let mask_u32 = LittleEndian::read_u32(mask); idx += 4; Some(mask_u32) } else { @@ -185,8 +183,7 @@ impl Frame { } let mask: &[u8] = &chunk[idx..idx + 4]; - let mask_u32: u32 = - unsafe { ptr::read_unaligned(mask.as_ptr() as *const u32) }; + let mask_u32 = LittleEndian::read_u32(mask); idx += 4; Some(mask_u32) } else { @@ -323,15 +320,10 @@ impl Frame { if genmask { let mask = rand::random::(); - unsafe { - { - let buf_mut = buf.bytes_mut(); - *(buf_mut as *mut _ as *mut u32) = mask; - buf_mut[4..payload_len + 4].copy_from_slice(payload.as_ref()); - apply_mask(&mut buf_mut[4..], mask); - } - buf.advance_mut(payload_len + 4); - } + buf.put_u32_le(mask); + buf.extend_from_slice(payload.as_ref()); + let pos = buf.len() - payload_len; + apply_mask(&mut buf[pos..], mask); buf.into() } else { buf.put_slice(payload.as_ref());