Skip to content

Commit 3fe48e3

Browse files
authored
Merge pull request #469 from Berrysoft/fix/next-release-unaligned
Fix unaligned read/write in bitmap backend, for next-release-devel.
2 parents 3137913 + da3b241 commit 3fe48e3

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

plotters-bitmap/src/bitmap_pixel/bgrx.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl PixelFormat for BGRXPixel {
8787
let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 2) };
8888
for rp in slice.iter_mut() {
8989
let ptr = rp as *mut [u8; 8] as *mut u64;
90-
let d1 = unsafe { *ptr };
90+
let d1 = unsafe { ptr.read_unaligned() };
9191
let mut h = (d1 >> 8) & M;
9292
let mut l = d1 & M;
9393

@@ -104,7 +104,7 @@ impl PixelFormat for BGRXPixel {
104104
}
105105

106106
unsafe {
107-
*ptr = h | l;
107+
ptr.write_unaligned(h | l);
108108
}
109109
}
110110

@@ -196,7 +196,7 @@ impl PixelFormat for BGRXPixel {
196196
let d: u64 = std::mem::transmute([
197197
b, g, r, 0, b, g, r, 0, // QW1
198198
]);
199-
*ptr = d;
199+
ptr.write_unaligned(d);
200200
}
201201
}
202202

plotters-bitmap/src/bitmap_pixel/rgb.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl PixelFormat for RGBPixel {
6767
// Since we should always make sure the RGB payload occupies the logic lower bits
6868
// thus, this type purning should work for both LE and BE CPUs
6969
#[rustfmt::skip]
70-
let (p1, p2, p3): (u64, u64, u64) = unsafe {
70+
let [p1, p2, p3]: [u64; 3] = unsafe {
7171
std::mem::transmute([
7272
u16::from(r), u16::from(b), u16::from(g), u16::from(r), // QW1
7373
u16::from(b), u16::from(g), u16::from(r), u16::from(b), // QW2
@@ -76,7 +76,7 @@ impl PixelFormat for RGBPixel {
7676
};
7777

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

@@ -120,7 +120,7 @@ impl PixelFormat for RGBPixel {
120120
}
121121

122122
unsafe {
123-
*ptr = (h1 | l1, h2 | l2, h3 | l3);
123+
ptr.write_unaligned([h1 | l1, h2 | l2, h3 | l3]);
124124
}
125125
}
126126

@@ -207,14 +207,14 @@ impl PixelFormat for RGBPixel {
207207
// TODO: Consider using AVX instructions when possible
208208
let ptr = p as *mut [u8; 24] as *mut u64;
209209
unsafe {
210-
let (d1, d2, d3): (u64, u64, u64) = std::mem::transmute([
210+
let [d1, d2, d3]: [u64; 3] = std::mem::transmute([
211211
r, g, b, r, g, b, r, g, // QW1
212212
b, r, g, b, r, g, b, r, // QW2
213213
g, b, r, g, b, r, g, b, // QW3
214214
]);
215-
*ptr = d1;
216-
*ptr.offset(1) = d2;
217-
*ptr.offset(2) = d3;
215+
ptr.write_unaligned(d1);
216+
ptr.offset(1).write_unaligned(d2);
217+
ptr.offset(2).write_unaligned(d3);
218218
}
219219
}
220220

0 commit comments

Comments
 (0)