Skip to content

Commit 4af8249

Browse files
authored
Merge pull request #490 from kcking/main
Migrate from mbox to malloced
2 parents e3ea42c + 95dcb6c commit 4af8249

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

tss-esapi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name = "hmac"
1818
[dependencies]
1919
bitfield = "0.13.2"
2020
serde = { version = "1.0.115", features = ["derive"] }
21-
mbox = "0.6.0"
21+
malloced = "1.3.1"
2222
log = "0.4.11"
2323
enumflags2 = "0.7.7"
2424
num-derive = "0.3.2"

tss-esapi/src/context.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
};
1414
use handle_manager::HandleManager;
1515
use log::{debug, error};
16-
use mbox::MBox;
16+
use malloced::Malloced;
1717
use std::collections::HashMap;
1818
use std::ptr::null_mut;
1919

@@ -47,7 +47,7 @@ use std::ptr::null_mut;
4747
pub struct Context {
4848
/// Handle for the ESYS context object owned through an Mbox.
4949
/// Wrapping the handle in an optional Mbox is done to allow the `Context` to be closed properly when the `Context` structure is dropped.
50-
esys_context: Option<MBox<ESYS_CONTEXT>>,
50+
esys_context: Option<Malloced<ESYS_CONTEXT>>,
5151
sessions: (
5252
Option<AuthSession>,
5353
Option<AuthSession>,
@@ -105,7 +105,7 @@ impl Context {
105105
},
106106
)?;
107107

108-
let esys_context = unsafe { Some(MBox::from_raw(esys_context)) };
108+
let esys_context = unsafe { Some(Malloced::from_raw(esys_context)) };
109109
Ok(Context {
110110
esys_context,
111111
sessions: (None, None, None),
@@ -390,7 +390,7 @@ impl Context {
390390
fn mut_context(&mut self) -> *mut ESYS_CONTEXT {
391391
self.esys_context
392392
.as_mut()
393-
.map(MBox::<ESYS_CONTEXT>::as_mut_ptr)
393+
.map(Malloced::<ESYS_CONTEXT>::as_mut_ptr)
394394
.unwrap() // will only fail if called from Drop after .take()
395395
}
396396

@@ -440,8 +440,12 @@ impl Context {
440440

441441
/// Private function for handling that has been allocated with
442442
/// C memory allocation functions in TSS.
443-
fn ffi_data_to_owned<T>(data_ptr: *mut T) -> T {
444-
MBox::into_inner(unsafe { MBox::from_raw(data_ptr) })
443+
fn ffi_data_to_owned<T: Copy>(data_ptr: *mut T) -> T {
444+
let out = unsafe { *data_ptr };
445+
446+
// Free the malloced data.
447+
drop(unsafe { Malloced::from_raw(data_ptr) });
448+
out
445449
}
446450
}
447451

@@ -476,7 +480,7 @@ impl Drop for Context {
476480
&mut self
477481
.esys_context
478482
.take()
479-
.map(MBox::<ESYS_CONTEXT>::into_raw)
483+
.map(Malloced::<ESYS_CONTEXT>::into_raw)
480484
.unwrap(), // should not fail based on how the context is initialised/used
481485
)
482486
};

tss-esapi/src/ffi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod data_zeroize;
55

66
use crate::{ffi::data_zeroize::FfiDataZeroize, Error, Result, WrapperErrorKind};
77
use log::error;
8-
use mbox::MBox;
8+
use malloced::Malloced;
99
use std::{convert::TryFrom, ops::Deref};
1010

1111
/// Function that takes ownership of data that has been
@@ -21,7 +21,7 @@ pub(crate) fn to_owned_with_zeroized_source<T>(ffi_data_ptr: *mut T) -> T
2121
where
2222
T: FfiDataZeroize + Copy,
2323
{
24-
let mut ffi_data = unsafe { MBox::from_raw(ffi_data_ptr) };
24+
let mut ffi_data = unsafe { Malloced::from_raw(ffi_data_ptr) };
2525
let owned_ffi_data: T = *ffi_data.deref();
2626
ffi_data.ffi_data_zeroize();
2727
owned_ffi_data
@@ -37,7 +37,7 @@ where
3737
/// # Returns
3838
/// The owned bytes in the form of a `Vec<u8>` object.
3939
pub fn to_owned_bytes(ffi_bytes_ptr: *mut u8, size: usize) -> Vec<u8> {
40-
let ffi_bytes = unsafe { MBox::<[u8]>::from_raw_parts(ffi_bytes_ptr, size) };
40+
let ffi_bytes = unsafe { Malloced::<[u8]>::slice_from_raw_parts(ffi_bytes_ptr, size) };
4141
return Vec::<u8>::from(ffi_bytes.as_ref());
4242
}
4343

0 commit comments

Comments
 (0)