From c5c2a4ab90e949a8bd4bf400b87a43989b70c1ad Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 14 Nov 2020 21:27:47 +0100 Subject: [PATCH] Improve buffer handling when interfacing with native layer. Fix #431 --- src/clipboard/macos.rs | 2 +- src/clipboard/windows.rs | 2 +- src/system/linux.rs | 12 ++++++------ src/system/macos.rs | 8 ++++---- src/system/windows.rs | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/clipboard/macos.rs b/src/clipboard/macos.rs index 44f7161..a6ef7e5 100644 --- a/src/clipboard/macos.rs +++ b/src/clipboard/macos.rs @@ -29,7 +29,7 @@ impl super::ClipboardManager for MacClipboardManager { fn get_clipboard(&self) -> Option { unsafe { let mut buffer: [c_char; 2000] = [0; 2000]; - let res = get_clipboard(buffer.as_mut_ptr(), buffer.len() as i32); + let res = get_clipboard(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = CStr::from_ptr(buffer.as_ptr()); diff --git a/src/clipboard/windows.rs b/src/clipboard/windows.rs index efe69ee..2341bcf 100644 --- a/src/clipboard/windows.rs +++ b/src/clipboard/windows.rs @@ -35,7 +35,7 @@ impl super::ClipboardManager for WindowsClipboardManager { fn get_clipboard(&self) -> Option { unsafe { let mut buffer: [u16; 2000] = [0; 2000]; - let res = get_clipboard(buffer.as_mut_ptr(), buffer.len() as i32); + let res = get_clipboard(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = U16CString::from_ptr_str(buffer.as_ptr()); diff --git a/src/system/linux.rs b/src/system/linux.rs index 5f2c8dc..0f8b144 100644 --- a/src/system/linux.rs +++ b/src/system/linux.rs @@ -29,8 +29,8 @@ pub struct LinuxSystemManager {} impl super::SystemManager for LinuxSystemManager { fn get_current_window_title(&self) -> Option { unsafe { - let mut buffer: [c_char; 100] = [0; 100]; - let res = get_active_window_name(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [c_char; 256] = [0; 256]; + let res = get_active_window_name(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = CStr::from_ptr(buffer.as_ptr()); @@ -47,8 +47,8 @@ impl super::SystemManager for LinuxSystemManager { fn get_current_window_class(&self) -> Option { unsafe { - let mut buffer: [c_char; 100] = [0; 100]; - let res = get_active_window_class(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [c_char; 256] = [0; 256]; + let res = get_active_window_class(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = CStr::from_ptr(buffer.as_ptr()); @@ -65,8 +65,8 @@ impl super::SystemManager for LinuxSystemManager { fn get_current_window_executable(&self) -> Option { unsafe { - let mut buffer: [c_char; 100] = [0; 100]; - let res = get_active_window_executable(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [c_char; 256] = [0; 256]; + let res = get_active_window_executable(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = CStr::from_ptr(buffer.as_ptr()); diff --git a/src/system/macos.rs b/src/system/macos.rs index f6f6ec0..4480c3d 100644 --- a/src/system/macos.rs +++ b/src/system/macos.rs @@ -33,8 +33,8 @@ impl super::SystemManager for MacSystemManager { fn get_current_window_class(&self) -> Option { unsafe { - let mut buffer: [c_char; 250] = [0; 250]; - let res = get_active_app_identifier(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [c_char; 256] = [0; 256]; + let res = get_active_app_identifier(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = CStr::from_ptr(buffer.as_ptr()); @@ -51,8 +51,8 @@ impl super::SystemManager for MacSystemManager { fn get_current_window_executable(&self) -> Option { unsafe { - let mut buffer: [c_char; 250] = [0; 250]; - let res = get_active_app_bundle(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [c_char; 256] = [0; 256]; + let res = get_active_app_bundle(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = CStr::from_ptr(buffer.as_ptr()); diff --git a/src/system/windows.rs b/src/system/windows.rs index eb0d030..4d7eb27 100644 --- a/src/system/windows.rs +++ b/src/system/windows.rs @@ -31,8 +31,8 @@ impl WindowsSystemManager { impl super::SystemManager for WindowsSystemManager { fn get_current_window_title(&self) -> Option { unsafe { - let mut buffer: [u16; 100] = [0; 100]; - let res = get_active_window_name(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [u16; 256] = [0; 256]; + let res = get_active_window_name(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = U16CString::from_ptr_str(buffer.as_ptr()); @@ -51,8 +51,8 @@ impl super::SystemManager for WindowsSystemManager { fn get_current_window_executable(&self) -> Option { unsafe { - let mut buffer: [u16; 250] = [0; 250]; - let res = get_active_window_executable(buffer.as_mut_ptr(), buffer.len() as i32); + let mut buffer: [u16; 256] = [0; 256]; + let res = get_active_window_executable(buffer.as_mut_ptr(), (buffer.len() - 1) as i32); if res > 0 { let c_string = U16CString::from_ptr_str(buffer.as_ptr());