fix warning: unaligned_references

fix warning, when compiling with 1.53.0
```
warning: reference to packed field is unaligned
   --> src/vhost_user/message.rs:252:53
    |
252 |   unsafe { std::mem::transmute_copy::<u32, R>(&self.request) }
    |                                               ^^^^^^^^^^^^^
    |
    = 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)
```

Signed-off-by: wanglei <wllenyj@linux.alibaba.com>
This commit is contained in:
wanglei01 2021-07-29 11:30:49 +08:00 committed by Sergio Lopez
parent c1f77c778b
commit 488b3adc2f
2 changed files with 28 additions and 6 deletions

View file

@ -1 +1 @@
{"coverage_score": 82.2, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"}
{"coverage_score": 82.3, "exclude_path": "src/vhost_kern/", "crate_features": "vhost-user-master,vhost-user-slave"}

View file

@ -223,9 +223,8 @@ bitflags! {
/// Common message header for vhost-user requests and replies.
/// A vhost-user message consists of 3 header fields and an optional payload. All numbers are in the
/// machine native byte order.
#[allow(safe_packed_borrows)]
#[repr(packed)]
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Copy)]
pub(super) struct VhostUserMsgHeader<R: Req> {
request: u32,
flags: u32,
@ -233,6 +232,28 @@ pub(super) struct VhostUserMsgHeader<R: Req> {
_r: PhantomData<R>,
}
impl<R: Req> Debug for VhostUserMsgHeader<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Point")
.field("request", &{ self.request })
.field("flags", &{ self.flags })
.field("size", &{ self.size })
.finish()
}
}
impl<R: Req> Clone for VhostUserMsgHeader<R> {
fn clone(&self) -> VhostUserMsgHeader<R> {
*self
}
}
impl<R: Req> PartialEq for VhostUserMsgHeader<R> {
fn eq(&self, other: &Self) -> bool {
self.request == other.request && self.flags == other.flags && self.size == other.size
}
}
impl<R: Req> VhostUserMsgHeader<R> {
/// Create a new instance of `VhostUserMsgHeader`.
pub fn new(request: R, flags: u32, size: u32) -> Self {
@ -249,7 +270,7 @@ impl<R: Req> VhostUserMsgHeader<R> {
/// Get message type.
pub fn get_code(&self) -> R {
// It's safe because R is marked as repr(u32).
unsafe { std::mem::transmute_copy::<u32, R>(&self.request) }
unsafe { std::mem::transmute_copy::<u32, R>(&{ self.request }) }
}
/// Set message type.
@ -803,7 +824,6 @@ impl DescStateSplit {
}
/// Inflight I/O queue region for split virtqueues
#[allow(safe_packed_borrows)]
#[repr(packed)]
pub struct QueueRegionSplit {
/// Features flags of this region
@ -868,7 +888,6 @@ impl DescStatePacked {
}
/// Inflight I/O queue region for packed virtqueues
#[allow(safe_packed_borrows)]
#[repr(packed)]
pub struct QueueRegionPacked {
/// Features flags of this region
@ -994,7 +1013,10 @@ mod tests {
hdr.set_version(0x1);
assert!(hdr.is_valid());
// Test Debug, Clone, PartiaEq trait
assert_eq!(hdr, hdr.clone());
assert_eq!(hdr.clone().get_code(), hdr.get_code());
assert_eq!(format!("{:?}", hdr.clone()), format!("{:?}", hdr));
}
#[test]