Skip to content

Commit

Permalink
Merge pull request from GHSA-q669-2vfg-cxcg
Browse files Browse the repository at this point in the history
refactor: remove few unsafe code
  • Loading branch information
doitian committed Apr 23, 2020
2 parents 7a85450 + c47e935 commit adf8f0d
Show file tree
Hide file tree
Showing 9 changed files with 1,133 additions and 1,437 deletions.
17 changes: 14 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DEFAULT_GOAL:=help
SHELL = /bin/sh
MOLC := moleculec
MOLC_VERSION := 0.5.0
MOLC_VERSION := 0.6.0
VERBOSE := $(if ${CI},--verbose,)
CLIPPY_OPTS := -D warnings -D clippy::clone_on_ref_ptr -D clippy::enum_glob_use -D clippy::fallible_impl_from
CKB_TEST_ARGS := -c 4
Expand Down
20 changes: 13 additions & 7 deletions protocols/discovery/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,24 @@ impl DiscoveryMessage {
.as_bytes()
}

#[allow(clippy::cast_ptr_alignment)]
pub fn decode(data: &[u8]) -> Option<Self> {
let reader = packed::DiscoveryMessageReader::from_compatible_slice(data).ok()?;
match reader.payload().to_enum() {
packed::DiscoveryPayloadUnionReader::GetNodes(reader) => {
let le = reader.version().raw_data().as_ptr() as *const u32;
let version = u32::from_le(unsafe { *le });
let le = reader.count().raw_data().as_ptr() as *const u32;
let count = u32::from_le(unsafe { *le });
let version = {
let mut b = [0u8; 4];
b.copy_from_slice(reader.version().raw_data());
u32::from_le_bytes(b)
};
let count = {
let mut b = [0u8; 4];
b.copy_from_slice(reader.count().raw_data());
u32::from_le_bytes(b)
};
let listen_port = reader.listen_port().to_opt().map(|port_reader| {
let le = port_reader.raw_data().as_ptr() as *const u16;
u16::from_le(unsafe { *le })
let mut b = [0u8; 2];
b.copy_from_slice(port_reader.raw_data());
u16::from_le_bytes(b)
});
Some(DiscoveryMessage::GetNodes {
version,
Expand Down
17 changes: 12 additions & 5 deletions protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,24 @@ impl PingMessage {
.as_bytes()
}

#[allow(clippy::cast_ptr_alignment)]
fn decode(data: &[u8]) -> Option<PingPayload> {
let reader = packed::PingMessageReader::from_compatible_slice(data).ok()?;
match reader.payload().to_enum() {
packed::PingPayloadUnionReader::Ping(reader) => {
let le = reader.nonce().raw_data().as_ptr() as *const u32;
Some(PingPayload::Ping(u32::from_le(unsafe { *le })))
let nonce = {
let mut b = [0u8; 4];
b.copy_from_slice(reader.as_slice());
u32::from_le_bytes(b)
};
Some(PingPayload::Ping(nonce))
}
packed::PingPayloadUnionReader::Pong(reader) => {
let le = reader.nonce().raw_data().as_ptr() as *const u32;
Some(PingPayload::Pong(u32::from_le(unsafe { *le })))
let nonce = {
let mut b = [0u8; 4];
b.copy_from_slice(reader.as_slice());
u32::from_le_bytes(b)
};
Some(PingPayload::Pong(nonce))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion util/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "MIT"

[dependencies]
molecule = "=0.5.0"
molecule = "=0.6.0"
ckb-fixed-hash = { path = "../fixed-hash" }
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
bytes = { version="0.5.4", features = ["serde"] }
Expand Down
30 changes: 15 additions & 15 deletions util/types/src/conversion/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ impl Pack<packed::Uint32> for usize {
}

impl<'r> Unpack<u32> for packed::Uint32Reader<'r> {
#[allow(clippy::cast_ptr_alignment)]
fn unpack(&self) -> u32 {
let le = self.as_slice().as_ptr() as *const u32;
u32::from_le(unsafe { *le })
let mut b = [0u8; 4];
b.copy_from_slice(self.as_slice());
u32::from_le_bytes(b)
}
}
impl_conversion_for_entity_unpack!(u32, Uint32);

impl<'r> Unpack<u64> for packed::Uint64Reader<'r> {
#[allow(clippy::cast_ptr_alignment)]
fn unpack(&self) -> u64 {
let le = self.as_slice().as_ptr() as *const u64;
u64::from_le(unsafe { *le })
let mut b = [0u8; 8];
b.copy_from_slice(self.as_slice());
u64::from_le_bytes(b)
}
}
impl_conversion_for_entity_unpack!(u64, Uint64);

impl<'r> Unpack<u128> for packed::Uint128Reader<'r> {
#[allow(clippy::cast_ptr_alignment)]
fn unpack(&self) -> u128 {
let le = self.as_slice().as_ptr() as *const u128;
u128::from_le(unsafe { *le })
let mut b = [0u8; 16];
b.copy_from_slice(self.as_slice());
u128::from_le_bytes(b)
}
}
impl_conversion_for_entity_unpack!(u128, Uint128);
Expand Down Expand Up @@ -96,19 +96,19 @@ impl Pack<packed::BeUint32> for usize {
}

impl<'r> Unpack<u32> for packed::BeUint32Reader<'r> {
#[allow(clippy::cast_ptr_alignment)]
fn unpack(&self) -> u32 {
let be = self.as_slice().as_ptr() as *const u32;
u32::from_be(unsafe { *be })
let mut b = [0u8; 4];
b.copy_from_slice(self.as_slice());
u32::from_be_bytes(b)
}
}
impl_conversion_for_entity_unpack!(u32, BeUint32);

impl<'r> Unpack<u64> for packed::BeUint64Reader<'r> {
#[allow(clippy::cast_ptr_alignment)]
fn unpack(&self) -> u64 {
let be = self.as_slice().as_ptr() as *const u64;
u64::from_be(unsafe { *be })
let mut b = [0u8; 8];
b.copy_from_slice(self.as_slice());
u64::from_be_bytes(b)
}
}
impl_conversion_for_entity_unpack!(u64, BeUint64);
Expand Down
Loading

0 comments on commit adf8f0d

Please # to comment.