Skip to content

Commit

Permalink
Add return value checking when invoking c APIs (#41)
Browse files Browse the repository at this point in the history
During rcl_init, things could fail and result in
segfault.

In this patch, we check the return code of
c function calls and panic in case of error.
  • Loading branch information
LIYOU ZHOU authored Mar 21, 2023
1 parent 0ed365f commit 88c16ba
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions r2r/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::CStr;
use std::ffi::CString;
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
Expand All @@ -13,6 +14,19 @@ pub struct Context {
pub(crate) context_handle: Arc<Mutex<ContextHandle>>,
}

macro_rules! check_rcl_ret {
($ret:expr) => {
if $ret != RCL_RET_OK as i32 {
let err_str = rcutils_get_error_string();
// c_char's definition is architecture specific
// https://github.com/fede1024/rust-rdkafka/issues/121#issuecomment-486578947
let str_ptr = &(err_str.str_) as *const std::os::raw::c_char;
let error_msg = CStr::from_ptr(str_ptr);
panic!("{}", error_msg.to_str().expect("to_str() call failed"));
}
};
}

unsafe impl Send for Context {}

impl Context {
Expand All @@ -32,14 +46,14 @@ impl Context {
let is_valid = unsafe {
let allocator = rcutils_get_default_allocator();
let mut init_options = rcl_get_zero_initialized_init_options();
rcl_init_options_init(&mut init_options, allocator);
rcl_init(
check_rcl_ret!(rcl_init_options_init(&mut init_options, allocator));
check_rcl_ret!(rcl_init(
(c_args.len() - 1) as ::std::os::raw::c_int,
c_args.as_ptr(),
&init_options,
ctx.as_mut(),
);
rcl_init_options_fini(&mut init_options as *mut _);
));
check_rcl_ret!(rcl_init_options_fini(&mut init_options as *mut _));
rcl_context_is_valid(ctx.as_mut())
};

Expand Down

0 comments on commit 88c16ba

Please sign in to comment.