|
1 | 1 | use crate::error::*; |
2 | 2 | use core::ptr::NonNull; |
3 | 3 | use foreign_types::{ForeignType, ForeignTypeRef, Opaque}; |
| 4 | +use log::error; |
4 | 5 |
|
5 | 6 | use wolfcrypt_rs::*; |
6 | 7 |
|
@@ -29,34 +30,41 @@ macro_rules! define_foreign_type { |
29 | 30 | } |
30 | 31 |
|
31 | 32 | 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. |
34 | 35 | pub fn new(c_type: &mut $c_type) -> $struct_name { |
35 | 36 | unsafe { |
36 | 37 | let new_object: $struct_name = $struct_name::from_ptr(c_type); |
37 | 38 | new_object |
38 | 39 | } |
39 | 40 | } |
40 | 41 |
|
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. |
42 | 43 | pub fn init(&self) { |
43 | 44 | unsafe { check_if_zero($init_function(self.as_ptr())).unwrap() } |
44 | 45 | } |
45 | 46 | } |
46 | 47 | }; |
47 | 48 |
|
48 | | - // For types that also need Drop implementations |
49 | 49 | ($struct_name:ident, $ref_name:ident, $c_type:ty, drop($drop_fn:ident), $init_function:ident) => { |
50 | 50 | define_foreign_type!($struct_name, $ref_name, $c_type, $init_function); |
51 | 51 |
|
| 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. |
52 | 56 | impl Drop for $struct_name { |
53 | 57 | fn drop(&mut self) { |
54 | 58 | 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(()) => {} |
60 | 68 | } |
61 | 69 | } |
62 | 70 | } |
@@ -88,15 +96,27 @@ macro_rules! define_foreign_type_with_copy { |
88 | 96 | } |
89 | 97 | }; |
90 | 98 |
|
91 | | - // For types that also need Drop implementations |
92 | 99 | ($struct_name:ident, $ref_name:ident, $c_type:ty, drop($drop_fn:ident)) => { |
93 | 100 | define_foreign_type_with_copy!($struct_name, $ref_name, $c_type); |
94 | 101 |
|
| 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. |
95 | 106 | impl Drop for $struct_name { |
96 | 107 | fn drop(&mut self) { |
97 | 108 | unsafe { |
98 | 109 | 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 | + } |
100 | 120 | } |
101 | 121 | } |
102 | 122 | } |
|
0 commit comments