Skip to content

Commit

Permalink
get rid of unsafe code and fix soundness issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wwylele committed Mar 2, 2021
1 parent 9c41996 commit a535678
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions byte_struct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
pub use byte_struct_derive::{ByteStruct, ByteStructBE, ByteStructLE};
pub use generic_array::*;

use std::convert::TryInto;

/// A type that can be packed into or unpacked from fixed-size bytes, but the method is unknown yet.
pub trait ByteStructLen {
/// The length of the packed bytes of this type
Expand Down Expand Up @@ -408,17 +410,10 @@ macro_rules! byte_struct_array {
}
}
fn read_bytes_default_le(bytes: &[u8]) -> Self {
let mut pos = 0;
let len = T::BYTE_LEN;
let mut result: Self;
unsafe {
result = std::mem::uninitialized();
for i in 0 .. ($x) {
std::ptr::write(&mut result[i], <T>::read_bytes_default_le(&bytes[pos .. pos + len]));
pos += len;
}
}
result
(0 .. ($x)).map(|i| {
<T>::read_bytes_default_le(&bytes[i * len .. (i + 1) * len])
}).collect::<Vec<_>>().try_into().map_err(|_|()).unwrap()
}
fn write_bytes_default_be(&self, bytes: &mut [u8]) {
let mut pos = 0;
Expand All @@ -429,17 +424,10 @@ macro_rules! byte_struct_array {
}
}
fn read_bytes_default_be(bytes: &[u8]) -> Self {
let mut pos = 0;
let len = T::BYTE_LEN;
let mut result: Self;
unsafe {
result = std::mem::uninitialized();
for i in 0 .. ($x) {
std::ptr::write(&mut result[i], <T>::read_bytes_default_be(&bytes[pos .. pos + len]));
pos += len;
}
}
result
(0 .. ($x)).map(|i| {
<T>::read_bytes_default_be(&bytes[i * len .. (i + 1) * len])
}).collect::<Vec<_>>().try_into().map_err(|_|()).unwrap()
}
}
}
Expand Down

0 comments on commit a535678

Please sign in to comment.