Skip to content

Commit 16bcbcd

Browse files
tronicalDrakulix
authored andcommitted
Fix unaligned memory access when reading DRM events
Running with debug assertions enabled on armv7 (an stm32mp157) panics due to unaligned memory reads: thread 'main' panicked at 'misaligned pointer dereference: address must be a multiple of 0x8 but is 0xbec90b44', .../drm-0.9.0/src/control/mod.rs:906:34 Fix this by using the corresponding functions from core to safely copy the data (in C we'd do a memcpy).
1 parent 424981d commit 16bcbcd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/control/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,14 +1139,16 @@ impl Iterator for Events {
11391139

11401140
fn next(&mut self) -> Option<Event> {
11411141
if self.amount > 0 && self.i < self.amount {
1142-
let event = unsafe { &*(self.event_buf.as_ptr().add(self.i) as *const ffi::drm_event) };
1142+
let event_ptr = unsafe { self.event_buf.as_ptr().add(self.i) as *const ffi::drm_event };
1143+
let event = unsafe { std::ptr::read_unaligned(event_ptr) };
11431144
self.i += event.length as usize;
11441145
match event.type_ {
11451146
ffi::DRM_EVENT_VBLANK => {
11461147
#[allow(unknown_lints)]
11471148
#[allow(invalid_reference_casting)]
1148-
let vblank_event =
1149-
unsafe { &*(event as *const _ as *const ffi::drm_event_vblank) };
1149+
let vblank_event = unsafe {
1150+
std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank)
1151+
};
11501152
Some(Event::Vblank(VblankEvent {
11511153
frame: vblank_event.sequence,
11521154
time: Duration::new(
@@ -1161,8 +1163,9 @@ impl Iterator for Events {
11611163
ffi::DRM_EVENT_FLIP_COMPLETE => {
11621164
#[allow(unknown_lints)]
11631165
#[allow(invalid_reference_casting)]
1164-
let vblank_event =
1165-
unsafe { &*(event as *const _ as *const ffi::drm_event_vblank) };
1166+
let vblank_event = unsafe {
1167+
std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank)
1168+
};
11661169
Some(Event::PageFlip(PageFlipEvent {
11671170
frame: vblank_event.sequence,
11681171
duration: Duration::new(

0 commit comments

Comments
 (0)