summaryrefslogtreecommitdiff
path: root/bindings
diff options
context:
space:
mode:
authorzy <[email protected]>2023-09-27 08:38:05 +0000
committerzy <[email protected]>2023-09-27 08:38:05 +0000
commitd36b58d854ceb3fc7061a97aa1fc65e7bce9cfc9 (patch)
tree4564ef135f4d8c31ebe115c8855a637546885257 /bindings
parent72b455147ca89af0826cccb443afdbbc511182a5 (diff)
timeout_cb::call captures the panic caused by fn_ptr pointing to an invalid function.
code clean
Diffstat (limited to 'bindings')
-rw-r--r--bindings/rs-timeout/src/timeout.rs26
1 files changed, 10 insertions, 16 deletions
diff --git a/bindings/rs-timeout/src/timeout.rs b/bindings/rs-timeout/src/timeout.rs
index 193d860..d3382d4 100644
--- a/bindings/rs-timeout/src/timeout.rs
+++ b/bindings/rs-timeout/src/timeout.rs
@@ -1,4 +1,4 @@
-use std::ptr::NonNull;
+use std::{panic, ptr::NonNull};
use libc::c_void;
@@ -56,23 +56,17 @@ impl<'a, T> TimeoutCallBack<'a, T> {
let fn_ptr = Some(callback);
TimeoutCallBack { fn_ptr, arg }
}
- /// run callback, if callback is None, return false
- pub fn call(&mut self) -> bool {
- if let Some(callback) = self.fn_ptr {
- callback(&mut self.arg);
- return true;
- }
- false
- }
}
impl timeout_cb {
fn call(&self) -> bool {
if let Some(callback) = self.fn_ptr {
- unsafe {
+ let result = panic::catch_unwind(|| unsafe {
callback(self.arg);
+ });
+ if result.is_ok() {
+ return true;
}
- return true;
}
return false;
}
@@ -208,7 +202,7 @@ impl Timeout {
/// delete periodic timeout
/// No ownership of the Timeout object is passed in
-pub fn delete_to_periodic(to: &mut Timeout) {
+pub fn delete_to_periodic(to: &Timeout) {
if to.is_periodic() {
unsafe { timeout_del(to as *const _ as *mut timeout) };
}
@@ -313,7 +307,8 @@ impl TimeoutManager {
pub fn get_hz(&self) -> timeout_t {
unsafe { timeouts_hz(self.get_raw()) }
}
- /// return interval to next required update
+ /// return interval to next required update.
+ ///
/// careful for two case:
/// - return value could be u64::MAX, it means no timeout on timing wheel
/// - return value will always be less than the next most recent timeout expiration time
@@ -441,7 +436,7 @@ impl<'a> Iterator for NextIter<'a> {
#[cfg(test)]
#[allow(unused_variables)]
mod tests {
- use std::{rc::Rc, cell::RefCell};
+ use std::{cell::RefCell, rc::Rc};
use super::*;
@@ -580,6 +575,7 @@ mod tests {
assert!(timeout2.is_some());
}
+ // just for test
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Session {
pub session_id: String,
@@ -634,7 +630,5 @@ mod tests {
});
timeout.run_cb();
-
- // println!("end session_id: {}", session.session_id);
}
}