Skip to content

Fix unaligned memory access when reading DRM events #191

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
merged 2 commits into from
Mar 27, 2024
Merged
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
17 changes: 8 additions & 9 deletions src/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,14 +1139,14 @@ impl Iterator for Events {

fn next(&mut self) -> Option<Event> {
if self.amount > 0 && self.i < self.amount {
let event = unsafe { &*(self.event_buf.as_ptr().add(self.i) as *const ffi::drm_event) };
let event_ptr = unsafe { self.event_buf.as_ptr().add(self.i) as *const ffi::drm_event };
let event = unsafe { std::ptr::read_unaligned(event_ptr) };
self.i += event.length as usize;
match event.type_ {
ffi::DRM_EVENT_VBLANK => {
#[allow(unknown_lints)]
#[allow(invalid_reference_casting)]
let vblank_event =
unsafe { &*(event as *const _ as *const ffi::drm_event_vblank) };
let vblank_event = unsafe {
std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank)
};
Some(Event::Vblank(VblankEvent {
frame: vblank_event.sequence,
time: Duration::new(
Expand All @@ -1159,10 +1159,9 @@ impl Iterator for Events {
}))
}
ffi::DRM_EVENT_FLIP_COMPLETE => {
#[allow(unknown_lints)]
#[allow(invalid_reference_casting)]
let vblank_event =
unsafe { &*(event as *const _ as *const ffi::drm_event_vblank) };
let vblank_event = unsafe {
std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank)
};
Some(Event::PageFlip(PageFlipEvent {
frame: vblank_event.sequence,
duration: Duration::new(
Expand Down