Skip to content

Commit 0fdcdae

Browse files
5225225Licenser
authored andcommitted
Add some more debug asserts, fix misaligned reads
1 parent 65d30a6 commit 0fdcdae

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/stage2.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use value_trait::StaticNode;
88
#[cfg_attr(not(feature = "no-inline"), inline(always))]
99
#[allow(clippy::cast_ptr_alignment)]
1010
pub fn is_valid_true_atom(loc: &[u8]) -> bool {
11-
debug_assert!(loc.len() >= 8, "input too short to safely read a u64 from");
11+
debug_assert!(loc.len() >= 8, "loc too short for a u64 read");
1212

1313
// TODO is this expensive?
1414
let mut error: u64;
@@ -20,7 +20,7 @@ pub fn is_valid_true_atom(loc: &[u8]) -> bool {
2020

2121
// TODO: does this has the same effect as:
2222
// std::memcpy(&locval, loc, sizeof(uint64_t));
23-
let locval: u64 = *(loc.as_ptr().cast::<u64>());
23+
let locval: u64 = loc.as_ptr().cast::<u64>().read_unaligned();
2424

2525
error = (locval & MASK4) ^ TV;
2626
error |= u64::from(is_not_structural_or_whitespace(*loc.get_kinda_unchecked(4)));
@@ -45,6 +45,8 @@ macro_rules! get {
4545
#[cfg_attr(not(feature = "no-inline"), inline(always))]
4646
#[allow(clippy::cast_ptr_alignment, unused_unsafe)]
4747
pub fn is_valid_false_atom(loc: &[u8]) -> bool {
48+
debug_assert!(loc.len() >= 8, "loc too short for a u64 read");
49+
4850
// TODO: this is ugly and probably copies data every time
4951
let mut error: u64;
5052
unsafe {
@@ -54,7 +56,7 @@ pub fn is_valid_false_atom(loc: &[u8]) -> bool {
5456
const FV: u64 = 0x00_00_00_65_73_6c_61_66;
5557
const MASK5: u64 = 0x00_00_00_ff_ff_ff_ff_ff;
5658

57-
let locval: u64 = *(loc.as_ptr().cast::<u64>());
59+
let locval: u64 = loc.as_ptr().cast::<u64>().read_unaligned();
5860

5961
// FIXME the original code looks like this:
6062
// error = ((locval & mask5) ^ fv) as u32;
@@ -70,14 +72,16 @@ pub fn is_valid_false_atom(loc: &[u8]) -> bool {
7072
#[cfg_attr(not(feature = "no-inline"), inline(always))]
7173
#[allow(clippy::cast_ptr_alignment, unused_unsafe)]
7274
pub fn is_valid_null_atom(loc: &[u8]) -> bool {
75+
debug_assert!(loc.len() >= 8, "loc too short for a u64 read");
76+
7377
// TODO is this expensive?
7478
let mut error: u64;
7579
unsafe {
7680
//let nv: u64 = *(b"null ".as_ptr() as *const u64);
7781
// this is the same:
7882
const NV: u64 = 0x00_00_00_00_6c_6c_75_6e;
7983
const MASK4: u64 = 0x00_00_00_00_ff_ff_ff_ff;
80-
let locval: u64 = *(loc.as_ptr().cast::<u64>());
84+
let locval: u64 = loc.as_ptr().cast::<u64>().read_unaligned();
8185

8286
error = (locval & MASK4) ^ NV;
8387
error |= u64::from(is_not_structural_or_whitespace(*get!(loc, 4)));

0 commit comments

Comments
 (0)