8000 Release Windows clipboard memory locks after reading by voidsipp · Pull Request #699 · not-fl3/miniquad · GitHub
[go: up one dir, main page]

Skip to content
Open
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
81 changes: 80 additions & 1 deletion src/native/windows/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use winapi::shared::minwindef::HGLOBAL;
use winapi::um::winbase::{GlobalAlloc, GlobalLock, GlobalSize, GlobalUnlock, GMEM_MOVEABLE};
use winapi::um::winuser::CF_UNICODETEXT;
use winapi::um::winuser::{
Expand Down Expand Up @@ -25,6 +26,16 @@ impl Drop for ClipboardGuard {
}
}

struct GlobalUnlockGuard(HGLOBAL);

impl Drop for GlobalUnlockGuard {
fn drop(&mut self) {
unsafe {
GlobalUnlock(self.0);
}
}
}

unsafe fn get_raw_clipboard() -> Option<Vec<u16>> {
// https://docs.microsoft.com/en-us/windows/win32/dataxchg/about-the-clipboard

Expand All @@ -41,11 +52,16 @@ unsafe fn get_raw_clipboard() -> Option<Vec<u16>> {
return None;
}

read_clipboard_data(clipboard_data)
}

unsafe fn read_clipboard_data(clipboard_data: HGLOBAL) -> Option<Vec<u16>> {
let data_ptr = GlobalLock(clipboard_data) as *const u16;
if data_ptr.is_null() {
return None;
}
let data_size = GlobalSize(clipboard_data) as usize;
let _lock_guard = GlobalUnlockGuard(clipboard_data);
let data_size = GlobalSize(clipboard_data) / std::mem::size_of::<u16>();

let slice = std::slice::from_raw_parts(data_ptr, data_size);
let len = slice.iter().position(|b| *b == 0).unwrap_or(data_size);
Expand Down Expand Up @@ -100,3 +116,66 @@ impl crate::native::Clipboard for WindowsClipboard {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use winapi::um::winbase::{GlobalFlags, GlobalFree, GMEM_LOCKCOUNT};

struct Allocation(HGLOBAL);

impl Drop for Allocation {
fn drop(&mut self) {
unsafe {
GlobalFree(self.0);
}
}
}

#[test]
fn reading_clipboard_data_releases_the_memory_lock() {
unsafe {
let text = "hello 🦀\0ignored".encode_utf16().collect::<Vec<_>>();
let allocation = Allocation(GlobalAlloc(GMEM_MOVEABLE, text.len() * 2));
assert!(!allocation.0.is_null());
let data = GlobalLock(allocation.0) as *mut u16;
assert!(!data.is_null());
ptr::copy_nonoverlapping(text.as_ptr(), data, text.len());
GlobalUnlock(allocation.0);
for _ in 0..3 {
assert_eq!(
read_clipboard_data(allocation.0).unwrap(),
"hello 🦀".encode_utf16().collect::<Vec<_>>()
);
assert_eq!(GlobalFlags(allocation.0) & GMEM_LOCKCOUNT, 0);
}
}
}

#[test]
fn unterminated_clipboard_data_stays_within_the_allocation() {
unsafe {
let allocation = Allocation(GlobalAlloc(GMEM_MOVEABLE, 16));
assert!(!allocation.0.is_null());
let data = GlobalLock(allocation.0) as *mut u8;
assert!(!data.is_null());
let size = GlobalSize(allocation.0);
ptr::write_bytes(data, 0x41, size);
GlobalUnlock(allocation.0);
assert_eq!(
read_clipboard_data(allocation.0).unwrap(),
vec![0x4141; size / 2]
);
assert_eq!(GlobalFlags(allocation.0) & GMEM_LOCKCOUNT, 0);
}
}

#[test]
fn discarded_clipboard_data_does_not_get_locked() {
unsafe {
let allocation = Allocation(GlobalAlloc(GMEM_MOVEABLE, 0));
assert!(!allocation.0.is_null());
assert!(read_clipboard_data(allocation.0).is_none());
assert_eq!(GlobalFlags(allocation.0) & GMEM_LOCKCOUNT, 0);
}
}
}
Loading
0