mirror of
https://github.com/lahfir/agent-desktop.git
synced 2026-08-05 05:30:21 +00:00
fix(ffi): use dynamic error codes and fix Box::from_raw UB in action result
This commit is contained in:
parent
3cd32d49ef
commit
88fec35f5f
3 changed files with 18 additions and 15 deletions
|
|
@ -118,11 +118,11 @@ pub(crate) fn action_result_to_c(r: &CoreActionResult) -> AdActionResult {
|
|||
let states = if state.states.is_empty() {
|
||||
ptr::null_mut()
|
||||
} else {
|
||||
let mut ptrs: Vec<*mut std::os::raw::c_char> =
|
||||
let ptrs: Vec<*mut std::os::raw::c_char> =
|
||||
state.states.iter().map(|s| string_to_c(s)).collect();
|
||||
ptrs.shrink_to_fit();
|
||||
let raw = ptrs.as_mut_ptr();
|
||||
std::mem::forget(ptrs);
|
||||
let mut boxed = ptrs.into_boxed_slice();
|
||||
let raw = boxed.as_mut_ptr();
|
||||
std::mem::forget(boxed);
|
||||
raw
|
||||
};
|
||||
let elem = Box::new(AdElementState {
|
||||
|
|
@ -189,7 +189,7 @@ pub unsafe extern "C" fn ad_resolve_element(
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrElementNotFound
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -229,7 +229,7 @@ pub unsafe extern "C" fn ad_execute_action(
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrActionFailed
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -255,10 +255,10 @@ pub unsafe extern "C" fn ad_free_action_result(result: *mut AdActionResult) {
|
|||
for ptr in slice.iter() {
|
||||
free_c_string(*ptr);
|
||||
}
|
||||
drop(Box::from_raw(
|
||||
std::slice::from_raw_parts_mut(state.states, state.state_count as usize)
|
||||
.as_mut_ptr(),
|
||||
));
|
||||
drop(Box::from_raw(std::ptr::slice_from_raw_parts_mut(
|
||||
state.states,
|
||||
state.state_count as usize,
|
||||
)));
|
||||
}
|
||||
drop(Box::from_raw(r.post_state));
|
||||
r.post_state = ptr::null_mut();
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ pub unsafe extern "C" fn ad_get_clipboard(
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrActionFailed
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -59,7 +59,7 @@ pub unsafe extern "C" fn ad_set_clipboard(
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrActionFailed
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -77,7 +77,7 @@ pub unsafe extern "C" fn ad_clear_clipboard(adapter: *const AdAdapter) -> AdResu
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrActionFailed
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -135,7 +135,7 @@ pub unsafe extern "C" fn ad_mouse_event(
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrActionFailed
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -173,7 +173,7 @@ pub unsafe extern "C" fn ad_drag(
|
|||
}
|
||||
Err(e) => {
|
||||
error::set_last_error(&e);
|
||||
AdResult::ErrActionFailed
|
||||
error::last_error_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -144,6 +144,9 @@ pub unsafe extern "C" fn ad_get_tree(
|
|||
opts: *const AdTreeOptions,
|
||||
out: *mut AdNodeTree,
|
||||
) -> AdResult {
|
||||
(*out).nodes = ptr::null_mut();
|
||||
(*out).count = 0;
|
||||
|
||||
let adapter = &*adapter;
|
||||
let opts_ref = &*opts;
|
||||
let core_win = crate::windows::ad_window_to_core(&*win);
|
||||
|
|
|
|||
Loading…
Reference in a new issue