mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-04-12 17:51:29 +03:00
fix: potential memleak (#10955)
Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
@@ -46,7 +46,7 @@ declare_class!(
|
|||||||
match std::fs::File::create(&path) {
|
match std::fs::File::create(&path) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let url = format!("file:///{}", &path);
|
let url = format!("file:///{}", &path);
|
||||||
item.setString_forType(&NSString::from_str(&url), &NSPasteboardTypeFileURL);
|
item.setString_forType(&NSString::from_str(&url), &NSPasteboardTypeFileURL);
|
||||||
let mut task_info = self.ivars().task_info.clone();
|
let mut task_info = self.ivars().task_info.clone();
|
||||||
task_info.source_path = path;
|
task_info.source_path = path;
|
||||||
self.ivars().tx.send(Ok(task_info)).ok();
|
self.ivars().tx.send(Ok(task_info)).ok();
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use crate::{
|
|||||||
send_data, ClipboardFile, CliprdrError, CliprdrServiceContext, ProgressPercent,
|
send_data, ClipboardFile, CliprdrError, CliprdrServiceContext, ProgressPercent,
|
||||||
};
|
};
|
||||||
use hbb_common::{allow_err, bail, log, ResultType};
|
use hbb_common::{allow_err, bail, log, ResultType};
|
||||||
use objc2::{msg_send_id, rc::Id, runtime::ProtocolObject, ClassType};
|
use objc2::{msg_send_id, rc::autoreleasepool, rc::Id, runtime::ProtocolObject, ClassType};
|
||||||
use objc2_app_kit::{NSPasteboard, NSPasteboardTypeFileURL};
|
use objc2_app_kit::{NSPasteboard, NSPasteboardTypeFileURL};
|
||||||
use objc2_foundation::{NSArray, NSString};
|
use objc2_foundation::{NSArray, NSString};
|
||||||
use std::{
|
use std::{
|
||||||
@@ -262,32 +262,7 @@ impl PasteboardContext {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
let tx = tx_handle.tx.clone();
|
autoreleasepool(|_| self.set_clipboard_item(tx_handle, conn_id, file_descriptor_id))?;
|
||||||
let provider = create_pasteboard_file_url_provider(
|
|
||||||
PasteObserverInfo {
|
|
||||||
file_descriptor_id,
|
|
||||||
conn_id,
|
|
||||||
source_path: "".to_string(),
|
|
||||||
target_path: "".to_string(),
|
|
||||||
},
|
|
||||||
tx,
|
|
||||||
);
|
|
||||||
unsafe {
|
|
||||||
let types = NSArray::from_vec(vec![NSString::from_str(
|
|
||||||
&NSPasteboardTypeFileURL.to_string(),
|
|
||||||
)]);
|
|
||||||
let item = objc2_app_kit::NSPasteboardItem::new();
|
|
||||||
item.setDataProvider_forTypes(&ProtocolObject::from_id(provider), &types);
|
|
||||||
self.pasteboard.clearContents();
|
|
||||||
if !self
|
|
||||||
.pasteboard
|
|
||||||
.writeObjects(&Id::cast(NSArray::from_vec(vec![item])))
|
|
||||||
{
|
|
||||||
return Err(CliprdrError::CommonError {
|
|
||||||
description: "failed to write objects".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return Err(CliprdrError::CommonError {
|
return Err(CliprdrError::CommonError {
|
||||||
description: "pasteboard context is not inited".to_string(),
|
description: "pasteboard context is not inited".to_string(),
|
||||||
@@ -296,6 +271,41 @@ impl PasteboardContext {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_clipboard_item(
|
||||||
|
&self,
|
||||||
|
tx_handle: &ContextInfo,
|
||||||
|
conn_id: i32,
|
||||||
|
file_descriptor_id: i32,
|
||||||
|
) -> Result<(), CliprdrError> {
|
||||||
|
let tx = tx_handle.tx.clone();
|
||||||
|
let provider = create_pasteboard_file_url_provider(
|
||||||
|
PasteObserverInfo {
|
||||||
|
file_descriptor_id,
|
||||||
|
conn_id,
|
||||||
|
source_path: "".to_string(),
|
||||||
|
target_path: "".to_string(),
|
||||||
|
},
|
||||||
|
tx,
|
||||||
|
);
|
||||||
|
unsafe {
|
||||||
|
let types = NSArray::from_vec(vec![NSString::from_str(
|
||||||
|
&NSPasteboardTypeFileURL.to_string(),
|
||||||
|
)]);
|
||||||
|
let item = objc2_app_kit::NSPasteboardItem::new();
|
||||||
|
item.setDataProvider_forTypes(&ProtocolObject::from_id(provider), &types);
|
||||||
|
self.pasteboard.clearContents();
|
||||||
|
if !self
|
||||||
|
.pasteboard
|
||||||
|
.writeObjects(&Id::cast(NSArray::from_vec(vec![item])))
|
||||||
|
{
|
||||||
|
return Err(CliprdrError::CommonError {
|
||||||
|
description: "failed to write objects".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_format_data_response(
|
fn handle_format_data_response(
|
||||||
&self,
|
&self,
|
||||||
conn_id: i32,
|
conn_id: i32,
|
||||||
@@ -427,6 +437,7 @@ mod tests {
|
|||||||
fn test_temp_files_count() {
|
fn test_temp_files_count() {
|
||||||
let mut c = super::PasteboardContext::temp_files_count();
|
let mut c = super::PasteboardContext::temp_files_count();
|
||||||
|
|
||||||
|
let mut created_files = vec![];
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
let path = format!(
|
let path = format!(
|
||||||
"/tmp/{}{}",
|
"/tmp/{}{}",
|
||||||
@@ -434,10 +445,16 @@ mod tests {
|
|||||||
uuid::Uuid::new_v4().to_string()
|
uuid::Uuid::new_v4().to_string()
|
||||||
);
|
);
|
||||||
if std::fs::File::create(&path).is_ok() {
|
if std::fs::File::create(&path).is_ok() {
|
||||||
|
created_files.push(path);
|
||||||
c += 1;
|
c += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(c, super::PasteboardContext::temp_files_count());
|
assert_eq!(c, super::PasteboardContext::temp_files_count());
|
||||||
|
|
||||||
|
// Clean up the created files.
|
||||||
|
for file in created_files {
|
||||||
|
std::fs::remove_file(&file).ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user