Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unaligned reference warnings on packed structures #2078

Closed
berkowski opened this issue Aug 23, 2021 · 1 comment · Fixed by #2079
Closed

Unaligned reference warnings on packed structures #2078

berkowski opened this issue Aug 23, 2021 · 1 comment · Fixed by #2079

Comments

@berkowski
Copy link

berkowski commented Aug 23, 2021

I'm getting unaligned_references warnings using serde_derive on packed structures. From #1747 and #1813 it seems like this should have been fixed since these structures also impl's Copy.

Here's a minimal example that reproduces what I'm seeing w/ serde 1.0.127

use serde;

mod other {
    #[repr(C, packed)]
    #[derive(Debug, Copy, Clone, PartialEq, Default)]
    pub struct paramval<'a> {
        pub val_or_len: i32,
        pub places: u8,
        pub type_: u8,
        pub buf: &'a [u8],
    }
}


#[derive(serde::Serialize, serde::Deserialize)]
#[serde(remote = "other::paramval")]
struct ParamVal<'a> {
    val_or_len: i32,
    places: u8,
    type_: u8,
    buf: &'a [u8]
}

fn main() {}
cargo build
 
⣿
Standard Error

   Compiling playground v0.0.1 (/playground)
warning: reference to packed field is unaligned
  --> src/main.rs:15:10
   |
15 | #[derive(serde::Serialize, serde::Deserialize)]
   |          ^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(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 <https://github.com/rust-lang/rust/issues/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)
   = note: this warning originates in the derive macro `serde::Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: reference to packed field is unaligned
  --> src/main.rs:15:10
   |
15 | #[derive(serde::Serialize, serde::Deserialize)]
   |          ^^^^^^^^^^^^^^^^
   |
   = 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 <https://github.com/rust-lang/rust/issues/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)
   = note: this warning originates in the derive macro `serde::Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: struct is never constructed: `ParamVal`
  --> src/main.rs:17:8
   |
17 | struct ParamVal<'a> {
   |        ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 3 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 1.09s
     Running `target/debug/playground`

playground link

The packed structures are generated using bindgen and represent data received via a serialport stream. I could drop the packed repr generation in bindgen but that would prevent other users from using direct memory transmutes if they wanted to skip over the serde bindings.

@dtolnay
Copy link
Member

dtolnay commented Aug 23, 2021

Thanks for the report! I've published a fix in serde_derive 1.0.129.

Note you'll need to add repr(packed) or repr(C, packed) on your copy of the struct to match the remote one.

#[derive(Serialize, Deserialize)]
#[serde(remote = "other::paramval")]
#[repr(packed)]
struct ParamVal<'a> {
    val_or_len: i32,
    places: u8,
    type_: u8,
    buf: &'a [u8]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants