Skip to content

Unaligned reference warnings on packed structures #2078

@berkowski

Description

@berkowski

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions