-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zeroize: add (back) ZeroizeOnDrop
trait
#652
Comments
Would a PR for this be welcome? I would introduce a new derive called To get around the issue of requiring all fields to implement trait AssertZeroizedOnDrop {
fn zeroize_or_on_drop(&mut self);
}
impl<T: ZeroizedOnDrop> AssertZeroizedOnDrop for &mut T {
fn zeroize_or_on_drop(&mut self) {}
}
trait AssertZeroize {
fn zeroize_or_on_drop(&mut self);
}
impl<T: Zeroize> AssertZeroize for T {
fn zeroize_or_on_drop(&mut self) {
self.zeroize()
}
} This would allow us to do the following: #[derive(ZeroizedOnDrop)]
struct Test {
a: u32,
// Some type only implementing `ZeroizedOnDrop`.
b: MyType,
}
// Generated:
impl Drop for Test {
fn drop(&mut self) {
self.a.zeroize_or_on_drop();
self.b.zeroize_or_on_drop();
}
}
impl ZeroizedOnDrop for Test { } Alternativel I can also add a helper attribute #[derive(ZeroizedOnDrop)]
struct Test {
a: u32,
// Some type only implementing `ZeroizedOnDrop`.
#[zeroize(dropped)]
b: MyType,
}
// Generated:
impl Drop for Test {
fn drop(&mut self) {
struct __AssertZeroizedOnDrop<__T: ::zeroize::ZeroizedOnDrop>(::core::marker::PhantomData<__T>);
self.a.zeroize();
let _: __AssertZeroizedOnDrop<MyType>;
}
}
impl ZeroizedOnDrop for Test { } WDYT? |
I think I'd rather just go back to the previous implementation from iqlusioninc/crates#168 which was removed in iqlusioninc/crates#188 and not introduce a whole lot of additional complexity. Beyond that, yes a PR would be welcome. Edit: there's one change I think would be beneficial versus the previous implementation... It should write a That would make it possible to impl If it's desirable for a type to have both, |
Sounds good.
That is indeed what I was proposing above.
So are we going to remove
So the reason I added the auto-deref example above is that there is no real other way to pass down a way to derive #[derive(ZeroizeOnDrop)]
struct TestA {
a: u8,
b: u8,
}
#[derive(ZeroizeOnDrop)]
struct TestB {
// `TestA` doesn't implement `Zeroize`, can't make the derive work.
#[zeroize(skip)]
a: TestA,
b: u8,
} The only way I can see us fix it is to introduce that auto-deref hack above. It is an implementation detail anyway, not something exposed to a user. I'm gonna start on an implementation without the auto-deref for now. |
I think that'd be fine eventually, although maybe as a separate PR at the very least.
Your code example is exactly what I'd expect |
But skipping actually doesn't check if it implements I suggest I make a PR for |
That's not necessarily desirable in all cases.
Sounds good |
I agree, that's why I would prefer the auto-deref implementation, it requires no further input from the user. In any case, we can do that later. |
The
zeroize
crate used to have aZeroizeOnDrop
trait: iqlusioninc/crates#168It was removed in favor of
#[zeroize(drop)]
which largely turned out to be a mistake: iqlusioninc/crates#188Originally filed by @fjarri as iqlusioninc/crates#757:
The text was updated successfully, but these errors were encountered: