Skip to content

Commit da9dff9

Browse files
committed
refactor: replace panic with error logging in Drop implementations
Improves safety by loggin cleanup failures instead of panicking during resource drops. This prevents crashed while preversing error information for debugging.
1 parent 41e3e45 commit da9dff9

File tree

1 file changed

+23
-11
lines changed
  • rustls-wolfcrypt-provider/src/types

1 file changed

+23
-11
lines changed

rustls-wolfcrypt-provider/src/types/mod.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::error::*;
22
use core::ptr::NonNull;
33
use foreign_types::{ForeignType, ForeignTypeRef, Opaque};
4+
use log::error;
45

56
use wolfcrypt_rs::*;
67

@@ -29,34 +30,37 @@ macro_rules! define_foreign_type {
2930
}
3031

3132
impl $struct_name {
32-
// Given a $c_type (FFI C binding), it creates an object around it
33-
// using the ForeignType's function from_ptr function.
33+
/// Given a $c_type (FFI C binding), it creates an object around it
34+
/// using the ForeignType's function from_ptr function.
3435
pub fn new(c_type: &mut $c_type) -> $struct_name {
3536
unsafe {
3637
let new_object: $struct_name = $struct_name::from_ptr(c_type);
3738
new_object
3839
}
3940
}
4041

41-
// Given an $init_function, it calls it with the object's ptr as argument.
42+
/// Given an $init_function, it calls it with the object's ptr as argument.
4243
pub fn init(&self) {
4344
unsafe { check_if_zero($init_function(self.as_ptr())).unwrap() }
4445
}
4546
}
4647
};
4748

48-
// For types that also need Drop implementations
4949
($struct_name:ident, $ref_name:ident, $c_type:ty, drop($drop_fn:ident), $init_function:ident) => {
5050
define_foreign_type!($struct_name, $ref_name, $c_type, $init_function);
5151

52+
/// Implements Drop trait for cryptographic types that require cleanup.
53+
/// This safely frees memory and other resources when the type goes out of scope.
54+
/// Any cleanup errors are logged but cannot be returned since this is Drop.
55+
/// The unsafe block is needed for FFI calls to the underlying C functions.
5256
impl Drop for $struct_name {
5357
fn drop(&mut self) {
5458
let ret = unsafe { $drop_fn(self.as_ptr()) };
55-
if ret != 0 {
56-
panic!(
57-
"Error while freeing resource in Drop for {}",
58-
stringify!($struct_name)
59-
);
59+
match check_if_zero(ret) {
60+
Err(err) => {
61+
error!("Error while freeing resource in Drop for {}: {}", stringify!($struct_name), err);
62+
}
63+
Ok(()) => {}
6064
}
6165
}
6266
}
@@ -88,15 +92,23 @@ macro_rules! define_foreign_type_with_copy {
8892
}
8993
};
9094

91-
// For types that also need Drop implementations
9295
($struct_name:ident, $ref_name:ident, $c_type:ty, drop($drop_fn:ident)) => {
9396
define_foreign_type_with_copy!($struct_name, $ref_name, $c_type);
9497

98+
/// Implements Drop trait for cryptographic types that require cleanup.
99+
/// This safely frees memory and other resources when the type goes out of scope.
100+
/// Any cleanup errors are logged but cannot be returned since this is Drop.
101+
/// The unsafe block is needed for FFI calls to the underlying C functions.
95102
impl Drop for $struct_name {
96103
fn drop(&mut self) {
97104
unsafe {
98105
let ret = $drop_fn(self.as_ptr());
99-
check_if_zero(ret).unwrap()
106+
match check_if_zero(ret) {
107+
Err(err) => {
108+
error!("Error while freeing resource in Drop for {}: {}", stringify!($struct_name), err);
109+
}
110+
Ok(()) => {}
111+
}
100112
}
101113
}
102114
}

0 commit comments

Comments
 (0)