Skip to content

Commit eecacb4

Browse files
committed
feat(try): make bailout unsafe and explain why
1 parent 0ab1aea commit eecacb4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/zend/try_catch.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ pub fn try_catch<R, F: FnMut() -> R + RefUnwindSafe>(func: F) -> Result<R, Catch
5656
///
5757
/// This function will stop the execution of the current script
5858
/// and jump to the last try catch block
59-
pub fn bailout() {
60-
unsafe {
61-
ext_php_rs_zend_bailout();
62-
}
59+
///
60+
/// # Safety
61+
///
62+
/// This function is unsafe because it can cause memory leaks
63+
/// Since it will jump to the last try catch block, it will not call the destructor of the current scope
64+
///
65+
/// When using this function you should ensure that all the memory allocated in the current scope is released
66+
///
67+
pub unsafe fn bailout() {
68+
ext_php_rs_zend_bailout();
6369
}
6470

6571
#[cfg(feature = "embed")]
@@ -73,7 +79,10 @@ mod tests {
7379
fn test_catch() {
7480
Embed::run(|| {
7581
let catch = try_catch(|| {
76-
bailout();
82+
unsafe {
83+
bailout();
84+
}
85+
7786
assert!(false);
7887
});
7988

@@ -95,7 +104,9 @@ mod tests {
95104
#[test]
96105
fn test_bailout() {
97106
Embed::run(|| {
98-
bailout();
107+
unsafe {
108+
bailout();
109+
}
99110

100111
assert!(false);
101112
});
@@ -134,7 +145,9 @@ mod tests {
134145
let mut result = "foo".to_string();
135146
ptr = &mut result;
136147

137-
bailout();
148+
unsafe {
149+
bailout();
150+
}
138151
});
139152

140153
// Check that the string is never released

0 commit comments

Comments
 (0)