Skip to content

Commit 78af557

Browse files
authored
Merge pull request #111 from magnusuMET/bugfix/panic_safety
check all callbacks for panic safety
2 parents 109dd83 + d0cd7bd commit 78af557

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

src/error.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cell::RefCell;
22
use std::error::Error as StdError;
33
use std::fmt;
44
use std::ops::Index;
5+
use std::panic;
56
use std::ptr;
67

78
use lazy_static::lazy_static;
@@ -65,7 +66,7 @@ lazy_static! {
6566
}
6667

6768
extern "C" fn default_error_handler(estack: hid_t, _cdata: *mut c_void) -> herr_t {
68-
unsafe { H5Eprint2(estack, ptr::null_mut()) }
69+
panic::catch_unwind(|| unsafe { H5Eprint2(estack, ptr::null_mut()) }).unwrap_or(-1)
6970
}
7071

7172
impl SilenceErrors {
@@ -134,7 +135,7 @@ impl ErrorStack {
134135
extern "C" fn callback(
135136
_: c_uint, err_desc: *const H5E_error2_t, data: *mut c_void,
136137
) -> herr_t {
137-
unsafe {
138+
panic::catch_unwind(|| unsafe {
138139
let data = &mut *(data as *mut CallbackData);
139140
if data.err.is_some() {
140141
return 0;
@@ -154,7 +155,8 @@ impl ErrorStack {
154155
}
155156
}
156157
0
157-
}
158+
})
159+
.unwrap_or(-1)
158160
}
159161

160162
let mut data = CallbackData { stack: Self::new(), err: None };

src/hl/group.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::fmt::{self, Debug};
22
use std::ops::Deref;
3+
use std::panic;
34

45
use hdf5_sys::{
56
h5::{hsize_t, H5_index_t, H5_iter_order_t},
@@ -177,11 +178,14 @@ impl Group {
177178
extern "C" fn members_callback(
178179
_id: hid_t, name: *const c_char, _info: *const H5L_info_t, op_data: *mut c_void,
179180
) -> herr_t {
180-
let other_data: &mut Vec<String> = unsafe { &mut *(op_data as *mut Vec<String>) };
181+
panic::catch_unwind(|| {
182+
let other_data: &mut Vec<String> = unsafe { &mut *(op_data as *mut Vec<String>) };
181183

182-
other_data.push(string_from_cstr(name));
184+
other_data.push(string_from_cstr(name));
183185

184-
0 // Continue iteration
186+
0 // Continue iteration
187+
})
188+
.unwrap_or(-1)
185189
}
186190

187191
let callback_fn: H5L_iterate_t = Some(members_callback);

src/hl/plist.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@ impl PropertyList {
169169
/// Iterates over properties in the property list, returning their names.
170170
pub fn properties(&self) -> Vec<String> {
171171
extern "C" fn callback(_: hid_t, name: *const c_char, data: *mut c_void) -> herr_t {
172-
let data = unsafe { &mut *(data as *mut Vec<String>) };
173-
let name = string_from_cstr(name);
174-
if !name.is_empty() {
175-
data.push(name);
176-
}
177-
0
172+
panic::catch_unwind(|| {
173+
let data = unsafe { &mut *(data as *mut Vec<String>) };
174+
let name = string_from_cstr(name);
175+
if !name.is_empty() {
176+
data.push(name);
177+
}
178+
0
179+
})
180+
.unwrap_or(-1)
178181
}
179182

180183
let mut data = Vec::new();

0 commit comments

Comments
 (0)