Skip to content

Fix unaligned read/write in bitmap backend, for next-release-devel. #469

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plotters-bitmap/src/bitmap_pixel/bgrx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl PixelFormat for BGRXPixel {
let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 2) };
for rp in slice.iter_mut() {
let ptr = rp as *mut [u8; 8] as *mut u64;
let d1 = unsafe { *ptr };
let d1 = unsafe { ptr.read_unaligned() };
let mut h = (d1 >> 8) & M;
let mut l = d1 & M;

Expand All @@ -104,7 +104,7 @@ impl PixelFormat for BGRXPixel {
}

unsafe {
*ptr = h | l;
ptr.write_unaligned(h | l);
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ impl PixelFormat for BGRXPixel {
let d: u64 = std::mem::transmute([
b, g, r, 0, b, g, r, 0, // QW1
]);
*ptr = d;
ptr.write_unaligned(d);
}
}

Expand Down
18 changes: 9 additions & 9 deletions plotters-bitmap/src/bitmap_pixel/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl PixelFormat for RGBPixel {
// Since we should always make sure the RGB payload occupies the logic lower bits
// thus, this type purning should work for both LE and BE CPUs
#[rustfmt::skip]
let (p1, p2, p3): (u64, u64, u64) = unsafe {
let [p1, p2, p3]: [u64; 3] = unsafe {
std::mem::transmute([
u16::from(r), u16::from(b), u16::from(g), u16::from(r), // QW1
u16::from(b), u16::from(g), u16::from(r), u16::from(b), // QW2
Expand All @@ -76,7 +76,7 @@ impl PixelFormat for RGBPixel {
};

#[rustfmt::skip]
let (q1, q2, q3): (u64, u64, u64) = unsafe {
let [q1, q2, q3]: [u64; 3] = unsafe {
std::mem::transmute([
u16::from(g), u16::from(r), u16::from(b), u16::from(g), // QW1
u16::from(r), u16::from(b), u16::from(g), u16::from(r), // QW2
Expand All @@ -94,8 +94,8 @@ impl PixelFormat for RGBPixel {
let start_ptr = &mut dst[start * Self::PIXEL_SIZE] as *mut u8 as *mut [u8; 24];
let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 8) };
for p in slice.iter_mut() {
let ptr = p as *mut [u8; 24] as *mut (u64, u64, u64);
let (d1, d2, d3) = unsafe { *ptr };
let ptr = p as *mut [u8; 24] as *mut [u64; 3];
let [d1, d2, d3] = unsafe { ptr.read_unaligned() };
let (mut h1, mut h2, mut h3) = ((d1 >> 8) & M, (d2 >> 8) & M, (d3 >> 8) & M);
let (mut l1, mut l2, mut l3) = (d1 & M, d2 & M, d3 & M);

Expand All @@ -120,7 +120,7 @@ impl PixelFormat for RGBPixel {
}

unsafe {
*ptr = (h1 | l1, h2 | l2, h3 | l3);
ptr.write_unaligned([h1 | l1, h2 | l2, h3 | l3]);
}
}

Expand Down Expand Up @@ -207,14 +207,14 @@ impl PixelFormat for RGBPixel {
// TODO: Consider using AVX instructions when possible
let ptr = p as *mut [u8; 24] as *mut u64;
unsafe {
let (d1, d2, d3): (u64, u64, u64) = std::mem::transmute([
let [d1, d2, d3]: [u64; 3] = std::mem::transmute([
r, g, b, r, g, b, r, g, // QW1
b, r, g, b, r, g, b, r, // QW2
g, b, r, g, b, r, g, b, // QW3
]);
*ptr = d1;
*ptr.offset(1) = d2;
*ptr.offset(2) = d3;
ptr.write_unaligned(d1);
ptr.offset(1).write_unaligned(d2);
ptr.offset(2).write_unaligned(d3);
}
}

Expand Down