Improve buffer handling when interfacing with native layer. Fix #431

This commit is contained in:
Federico Terzi 2020-11-14 21:27:47 +01:00
parent c4409241b6
commit c5c2a4ab90
5 changed files with 16 additions and 16 deletions

View File

@ -29,7 +29,7 @@ impl super::ClipboardManager for MacClipboardManager {
fn get_clipboard(&self) -> Option<String> { fn get_clipboard(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [c_char; 2000] = [0; 2000]; 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 { if res > 0 {
let c_string = CStr::from_ptr(buffer.as_ptr()); let c_string = CStr::from_ptr(buffer.as_ptr());

View File

@ -35,7 +35,7 @@ impl super::ClipboardManager for WindowsClipboardManager {
fn get_clipboard(&self) -> Option<String> { fn get_clipboard(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [u16; 2000] = [0; 2000]; 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 { if res > 0 {
let c_string = U16CString::from_ptr_str(buffer.as_ptr()); let c_string = U16CString::from_ptr_str(buffer.as_ptr());

View File

@ -29,8 +29,8 @@ pub struct LinuxSystemManager {}
impl super::SystemManager for LinuxSystemManager { impl super::SystemManager for LinuxSystemManager {
fn get_current_window_title(&self) -> Option<String> { fn get_current_window_title(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [c_char; 100] = [0; 100]; let mut buffer: [c_char; 256] = [0; 256];
let res = get_active_window_name(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_window_name(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = CStr::from_ptr(buffer.as_ptr()); 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<String> { fn get_current_window_class(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [c_char; 100] = [0; 100]; let mut buffer: [c_char; 256] = [0; 256];
let res = get_active_window_class(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_window_class(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = CStr::from_ptr(buffer.as_ptr()); 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<String> { fn get_current_window_executable(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [c_char; 100] = [0; 100]; let mut buffer: [c_char; 256] = [0; 256];
let res = get_active_window_executable(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_window_executable(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = CStr::from_ptr(buffer.as_ptr()); let c_string = CStr::from_ptr(buffer.as_ptr());

View File

@ -33,8 +33,8 @@ impl super::SystemManager for MacSystemManager {
fn get_current_window_class(&self) -> Option<String> { fn get_current_window_class(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [c_char; 250] = [0; 250]; let mut buffer: [c_char; 256] = [0; 256];
let res = get_active_app_identifier(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_app_identifier(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = CStr::from_ptr(buffer.as_ptr()); 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<String> { fn get_current_window_executable(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [c_char; 250] = [0; 250]; let mut buffer: [c_char; 256] = [0; 256];
let res = get_active_app_bundle(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_app_bundle(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = CStr::from_ptr(buffer.as_ptr()); let c_string = CStr::from_ptr(buffer.as_ptr());

View File

@ -31,8 +31,8 @@ impl WindowsSystemManager {
impl super::SystemManager for WindowsSystemManager { impl super::SystemManager for WindowsSystemManager {
fn get_current_window_title(&self) -> Option<String> { fn get_current_window_title(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [u16; 100] = [0; 100]; let mut buffer: [u16; 256] = [0; 256];
let res = get_active_window_name(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_window_name(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = U16CString::from_ptr_str(buffer.as_ptr()); 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<String> { fn get_current_window_executable(&self) -> Option<String> {
unsafe { unsafe {
let mut buffer: [u16; 250] = [0; 250]; let mut buffer: [u16; 256] = [0; 256];
let res = get_active_window_executable(buffer.as_mut_ptr(), buffer.len() as i32); let res = get_active_window_executable(buffer.as_mut_ptr(), (buffer.len() - 1) as i32);
if res > 0 { if res > 0 {
let c_string = U16CString::from_ptr_str(buffer.as_ptr()); let c_string = U16CString::from_ptr_str(buffer.as_ptr());