Skip to content

Commit 9cfb191

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 4ea667e commit 9cfb191

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

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

Lines changed: 31 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,41 @@ 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!(
62+
"Error while freeing resource in Drop for {}: {}",
63+
stringify!($struct_name),
64+
err
65+
);
66+
}
67+
Ok(()) => {}
6068
}
6169
}
6270
}
@@ -88,15 +96,27 @@ macro_rules! define_foreign_type_with_copy {
8896
}
8997
};
9098

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

102+
/// Implements Drop trait for cryptographic types that require cleanup.
103+
/// This safely frees memory and other resources when the type goes out of scope.
104+
/// Any cleanup errors are logged but cannot be returned since this is Drop.
105+
/// The unsafe block is needed for FFI calls to the underlying C functions.
95106
impl Drop for $struct_name {
96107
fn drop(&mut self) {
97108
unsafe {
98109
let ret = $drop_fn(self.as_ptr());
99-
check_if_zero(ret).unwrap()
110+
match check_if_zero(ret) {
111+
Err(err) => {
112+
error!(
113+
"Error while freeing resource in Drop for {}: {}",
114+
stringify!($struct_name),
115+
err
116+
);
117+
}
118+
Ok(()) => {}
119+
}
100120
}
101121
}
102122
}

wolfcrypt-rs/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::path::PathBuf;
88
use std::process::Command;
99

1010
/// Version-related constants for WolfSSL
11-
const WOLFSSL_DIR: &str = "wolfssl-5.7.4-stable";
12-
const WOLFSSL_ZIP: &str = "wolfssl-5.7.4-stable.zip";
13-
const WOLFSSL_URL: &str = "https://github.com/wolfSSL/wolfssl/archive/refs/tags/v5.7.4-stable.zip";
11+
const WOLFSSL_DIR: &str = "wolfssl-5.7.6-stable";
12+
const WOLFSSL_ZIP: &str = "wolfssl-5.7.6-stable.zip";
13+
const WOLFSSL_URL: &str = "https://github.com/wolfSSL/wolfssl/archive/refs/tags/v5.7.6-stable.zip";
1414

1515
/// Entry point for the build script.
1616
/// Handles the main build process and exits with an error code if anything fails.

0 commit comments

Comments
 (0)