aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2016-05-21 09:53:57 -0400
committerAndrew Gallant <jamslam@gmail.com>2016-05-21 09:53:57 -0400
commitf863c9eda89b5e2aa72d650991b815f9c960ed52 (patch)
tree0cf02fde5b5ba58ba948a95e656340f387d2c91b /src/lib.rs
parentd17987c1748d81472eb15204f269bccec75adab1 (diff)
downloadbyteorder-f863c9eda89b5e2aa72d650991b815f9c960ed52.tar.gz
Fixes undefined behavior reported in #47.
Instead of casting pointers, we do a proper unaligned load using copy_nonoverlapping. Benchmarks appear unaffected on Linux x64.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e4fddfe..5921bcb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -300,10 +300,16 @@ pub type NativeEndian = BigEndian;
macro_rules! read_num_bytes {
($ty:ty, $size:expr, $src:expr, $which:ident) => ({
+ assert!($size == ::std::mem::size_of::<$ty>());
assert!($size <= $src.len());
+ let mut data: $ty = 0;
unsafe {
- (*($src.as_ptr() as *const $ty)).$which()
+ copy_nonoverlapping(
+ $src.as_ptr(),
+ &mut data as *mut $ty as *mut u8,
+ $size);
}
+ data.$which()
});
}