fix portable service crash caused by unwrap Option

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-04-11 12:41:03 +08:00
parent 341f36caf2
commit ec66bf9c1f

View File

@ -479,12 +479,11 @@ pub mod client {
)?); )?);
shutdown_hooks::add_shutdown_hook(drop_portable_service_shared_memory); shutdown_hooks::add_shutdown_hook(drop_portable_service_shared_memory);
} }
let mut option = SHMEM.lock().unwrap(); if let Some(shmem) = SHMEM.lock().unwrap().as_mut() {
let shmem = option.as_mut().unwrap();
unsafe { unsafe {
libc::memset(shmem.as_ptr() as _, 0, shmem.len() as _); libc::memset(shmem.as_ptr() as _, 0, shmem.len() as _);
} }
drop(option); }
match para { match para {
StartPara::Direct => { StartPara::Direct => {
if let Err(e) = crate::platform::run_background( if let Err(e) = crate::platform::run_background(
@ -544,8 +543,11 @@ pub mod client {
} }
pub extern "C" fn drop_portable_service_shared_memory() { pub extern "C" fn drop_portable_service_shared_memory() {
let mut lock = SHMEM.lock().unwrap();
if lock.is_some() {
*lock = None;
log::info!("drop shared memory"); log::info!("drop shared memory");
*SHMEM.lock().unwrap() = None; }
} }
pub fn set_quick_support(v: bool) { pub fn set_quick_support(v: bool) {
@ -560,7 +562,7 @@ pub mod client {
Self: Sized, Self: Sized,
{ {
let mut option = SHMEM.lock().unwrap(); let mut option = SHMEM.lock().unwrap();
let shmem = option.as_mut().unwrap(); if let Some(shmem) = option.as_mut() {
Self::set_para( Self::set_para(
shmem, shmem,
CapturerPara { CapturerPara {
@ -571,6 +573,7 @@ pub mod client {
}, },
); );
shmem.write(ADDR_CAPTURE_WOULDBLOCK, &utils::i32_to_vec(TRUE)); shmem.write(ADDR_CAPTURE_WOULDBLOCK, &utils::i32_to_vec(TRUE));
}
CapturerPortable {} CapturerPortable {}
} }
@ -587,7 +590,7 @@ pub mod client {
impl TraitCapturer for CapturerPortable { impl TraitCapturer for CapturerPortable {
fn set_use_yuv(&mut self, use_yuv: bool) { fn set_use_yuv(&mut self, use_yuv: bool) {
let mut option = SHMEM.lock().unwrap(); let mut option = SHMEM.lock().unwrap();
let shmem = option.as_mut().unwrap(); if let Some(shmem) = option.as_mut() {
unsafe { unsafe {
let para_ptr = shmem.as_ptr().add(ADDR_CAPTURER_PARA); let para_ptr = shmem.as_ptr().add(ADDR_CAPTURER_PARA);
let para = para_ptr as *const CapturerPara; let para = para_ptr as *const CapturerPara;
@ -602,10 +605,14 @@ pub mod client {
); );
} }
} }
}
fn frame<'a>(&'a mut self, timeout: Duration) -> std::io::Result<Frame<'a>> { fn frame<'a>(&'a mut self, timeout: Duration) -> std::io::Result<Frame<'a>> {
let mut option = SHMEM.lock().unwrap(); let mut lock = SHMEM.lock().unwrap();
let shmem = option.as_mut().unwrap(); let shmem = lock.as_mut().ok_or(std::io::Error::new(
std::io::ErrorKind::Other,
"shmem dropped".to_string(),
))?;
unsafe { unsafe {
let base = shmem.as_ptr(); let base = shmem.as_ptr();
let para_ptr = base.add(ADDR_CAPTURER_PARA); let para_ptr = base.add(ADDR_CAPTURER_PARA);
@ -801,7 +808,10 @@ pub mod client {
pub fn get_cursor_info(pci: PCURSORINFO) -> BOOL { pub fn get_cursor_info(pci: PCURSORINFO) -> BOOL {
if RUNNING.lock().unwrap().clone() { if RUNNING.lock().unwrap().clone() {
get_cursor_info_(&mut SHMEM.lock().unwrap().as_mut().unwrap(), pci) let mut option = SHMEM.lock().unwrap();
option
.as_mut()
.map_or(FALSE, |sheme| get_cursor_info_(sheme, pci))
} else { } else {
unsafe { winuser::GetCursorInfo(pci) } unsafe { winuser::GetCursorInfo(pci) }
} }