Skip to content

Commit

Permalink
Fix reference to packed field is unaligned
Browse files Browse the repository at this point in the history
error: reference to packed field is unaligned
   --> src/codec.rs:187:35
    |
187 |                     let payload = &event.u.start;
    |                                   ^^^^^^^^^^^^^^
    |
    = note: `#[deny(unaligned_references)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #82523 <rust-lang/rust#82523>
    = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
    = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
  • Loading branch information
haata committed Aug 30, 2022
1 parent fb767ca commit 8e6d966
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ impl TryFrom<sys::uhid_event> for OutputEvent {
if let Some(event_type) = to_uhid_event_type(event.type_) {
match event_type {
sys::uhid_event_type_UHID_START => Ok(unsafe {
let payload = &event.u.start;
// Deal with unaligned packed struct access
let payload_rw = std::ptr::addr_of!(event.u.start);
let payload = payload_rw.read_unaligned();
OutputEvent::Start {
dev_flags: BitFlags::from_bits_truncate(payload.dev_flags)
.iter()
Expand Down

0 comments on commit 8e6d966

Please sign in to comment.