Skip to content

Commit 9270ee9

Browse files
authored
zeroize: formatting fixups (#770)
Reorder code in terms of precedence; add whitespace
1 parent 855d0ca commit 9270ee9

File tree

1 file changed

+139
-110
lines changed

1 file changed

+139
-110
lines changed

zeroize/src/lib.rs

Lines changed: 139 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -251,56 +251,38 @@ mod aarch64;
251251
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
252252
mod x86;
253253

254-
use core::marker::{PhantomData, PhantomPinned};
255-
use core::mem::{self, MaybeUninit};
256-
use core::num::{
257-
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
258-
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
254+
use core::{
255+
marker::{PhantomData, PhantomPinned},
256+
mem::{self, MaybeUninit},
257+
num::{
258+
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
259+
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
260+
},
261+
ops, ptr,
262+
slice::IterMut,
263+
sync::atomic,
259264
};
260-
use core::{ops, ptr, slice::IterMut, sync::atomic};
261265

262266
#[cfg(feature = "alloc")]
263-
use alloc::{boxed::Box, string::String, vec::Vec};
267+
use {
268+
alloc::{boxed::Box, string::String, vec::Vec},
269+
core::slice,
270+
};
264271

265272
#[cfg(feature = "std")]
266273
use std::ffi::CString;
267274

268-
/// Trait for securely erasing types from memory
275+
/// Trait for securely erasing values from memory.
269276
pub trait Zeroize {
270277
/// Zero out this object from memory using Rust intrinsics which ensure the
271278
/// zeroization operation is not "optimized away" by the compiler.
272279
fn zeroize(&mut self);
273280
}
274281

275-
/// Marker trait signifying that this type will [`zeroize`](Zeroize::zeroize) itself on [`Drop`].
282+
/// Marker trait signifying that this type will [`Zeroize::zeroize`] itself on [`Drop`].
276283
pub trait ZeroizeOnDrop {}
277284

278-
#[doc(hidden)]
279-
pub mod __internal {
280-
use super::*;
281-
282-
/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
283-
pub trait AssertZeroizeOnDrop {
284-
fn zeroize_or_on_drop(self);
285-
}
286-
287-
impl<T: ZeroizeOnDrop + ?Sized> AssertZeroizeOnDrop for &&mut T {
288-
fn zeroize_or_on_drop(self) {}
289-
}
290-
291-
/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
292-
pub trait AssertZeroize {
293-
fn zeroize_or_on_drop(&mut self);
294-
}
295-
296-
impl<T: Zeroize + ?Sized> AssertZeroize for T {
297-
fn zeroize_or_on_drop(&mut self) {
298-
self.zeroize()
299-
}
300-
}
301-
}
302-
303-
/// Marker trait for types whose `Default` is the desired zeroization result
285+
/// Marker trait for types whose [`Default`] is the desired zeroization result
304286
pub trait DefaultIsZeroes: Copy + Default + Sized {}
305287

306288
impl<Z> Zeroize for Z
@@ -319,19 +301,24 @@ macro_rules! impl_zeroize_with_default {
319301
};
320302
}
321303

322-
impl_zeroize_with_default!(i8, i16, i32, i64, i128, isize);
323-
impl_zeroize_with_default!(u8, u16, u32, u64, u128, usize);
324-
impl_zeroize_with_default!(f32, f64, char, bool);
304+
#[rustfmt::skip]
305+
impl_zeroize_with_default! {
306+
bool, char,
307+
f32, f64,
308+
i8, i16, i32, i64, i128, isize,
309+
u8, u16, u32, u64, u128, usize
310+
}
325311

326312
macro_rules! impl_zeroize_for_non_zero {
327313
($($type:ty),+) => {
328-
$(impl Zeroize for $type
329-
{
330-
fn zeroize(&mut self) {
331-
volatile_write(self, unsafe { <$type>::new_unchecked(1) });
332-
atomic_fence();
314+
$(
315+
impl Zeroize for $type {
316+
fn zeroize(&mut self) {
317+
volatile_write(self, unsafe { <$type>::new_unchecked(1) });
318+
atomic_fence();
319+
}
333320
}
334-
})+
321+
)+
335322
};
336323
}
337324

@@ -341,9 +328,7 @@ impl_zeroize_for_non_zero!(
341328
NonZeroI32,
342329
NonZeroI64,
343330
NonZeroI128,
344-
NonZeroIsize
345-
);
346-
impl_zeroize_for_non_zero!(
331+
NonZeroIsize,
347332
NonZeroU8,
348333
NonZeroU16,
349334
NonZeroU32,
@@ -352,7 +337,7 @@ impl_zeroize_for_non_zero!(
352337
NonZeroUsize
353338
);
354339

355-
/// Implement `Zeroize` on arrays of types that impl `Zeroize`
340+
/// Impl [`Zeroize`] on arrays of types that impl [`Zeroize`].
356341
impl<Z, const N: usize> Zeroize for [Z; N]
357342
where
358343
Z: Zeroize,
@@ -361,7 +346,8 @@ where
361346
self.iter_mut().zeroize();
362347
}
363348
}
364-
/// Implement `ZeroizeOnDrop` on arrays of types that impl `ZeroizeOnDrop`
349+
350+
/// Impl [`ZeroizeOnDrop`] on arrays of types that impl [`ZeroizeOnDrop`].
365351
impl<Z, const N: usize> ZeroizeOnDrop for [Z; N] where Z: ZeroizeOnDrop {}
366352

367353
impl<'a, Z> Zeroize for IterMut<'a, Z>
@@ -417,16 +403,21 @@ where
417403

418404
impl<Z> ZeroizeOnDrop for Option<Z> where Z: ZeroizeOnDrop {}
419405

420-
/// Impl `Zeroize` on slices of MaybeUninit types
406+
/// Impl [`Zeroize`] on slices of [`MaybeUninit`] types.
407+
///
421408
/// This impl can eventually be optimized using an memset intrinsic,
422-
/// such as `core::intrinsics::volatile_set_memory`.
423-
/// This fills the slice with zeros
424-
/// Note that this ignore invariants that Z might have, because MaybeUninit removes all invariants.
409+
/// such as [`core::intrinsics::volatile_set_memory`].
410+
///
411+
/// This fills the slice with zeroes.
412+
///
413+
/// Note that this ignore invariants that `Z` might have, because
414+
/// [`MaybeUninit`] removes all invariants.
425415
impl<Z> Zeroize for [MaybeUninit<Z>] {
426416
fn zeroize(&mut self) {
427417
let ptr = self.as_mut_ptr() as *mut MaybeUninit<u8>;
428418
let size = self.len().checked_mul(mem::size_of::<Z>()).unwrap();
429-
assert!(size <= core::isize::MAX as usize);
419+
assert!(size <= isize::MAX as usize);
420+
430421
// Safety:
431422
//
432423
// This is safe, because every valid pointer is well aligned for u8
@@ -438,20 +429,21 @@ impl<Z> Zeroize for [MaybeUninit<Z>] {
438429
}
439430
}
440431

441-
/// Impl `Zeroize` on slices of types that can be zeroized with `Default`.
432+
/// Impl [`Zeroize`] on slices of types that can be zeroized with [`Default`].
442433
///
443434
/// This impl can eventually be optimized using an memset intrinsic,
444-
/// such as `core::intrinsics::volatile_set_memory`. For that reason the blanket
445-
/// impl on slices is bounded by `DefaultIsZeroes`.
435+
/// such as [`core::intrinsics::volatile_set_memory`]. For that reason the
436+
/// blanket impl on slices is bounded by [`DefaultIsZeroes`].
446437
///
447438
/// To zeroize a mut slice of `Z: Zeroize` which does not impl
448-
/// `DefaultIsZeroes`, call `iter_mut().zeroize()`.
439+
/// [`DefaultIsZeroes`], call `iter_mut().zeroize()`.
449440
impl<Z> Zeroize for [Z]
450441
where
451442
Z: DefaultIsZeroes,
452443
{
453444
fn zeroize(&mut self) {
454-
assert!(self.len() <= core::isize::MAX as usize);
445+
assert!(self.len() <= isize::MAX as usize);
446+
455447
// Safety:
456448
//
457449
// This is safe, because the slice is well aligned and is backed by a single allocated
@@ -463,6 +455,65 @@ where
463455
}
464456
}
465457

458+
/// [`PhantomData`] is always zero sized so provide a [`Zeroize`] implementation.
459+
impl<Z> Zeroize for PhantomData<Z> {
460+
fn zeroize(&mut self) {}
461+
}
462+
463+
/// [`PhantomData` is always zero sized so provide a ZeroizeOnDrop implementation.
464+
impl<Z> ZeroizeOnDrop for PhantomData<Z> {}
465+
466+
/// `PhantomPinned` is zero sized so provide a Zeroize implementation.
467+
impl Zeroize for PhantomPinned {
468+
fn zeroize(&mut self) {}
469+
}
470+
471+
/// `PhantomPinned` is zero sized so provide a ZeroizeOnDrop implementation.
472+
impl ZeroizeOnDrop for PhantomPinned {}
473+
474+
/// `()` is zero sized so provide a Zeroize implementation.
475+
impl Zeroize for () {
476+
fn zeroize(&mut self) {}
477+
}
478+
479+
/// `()` is zero sized so provide a ZeroizeOnDrop implementation.
480+
impl ZeroizeOnDrop for () {}
481+
482+
/// Generic implementation of Zeroize for tuples up to 10 parameters.
483+
impl<A: Zeroize> Zeroize for (A,) {
484+
fn zeroize(&mut self) {
485+
self.0.zeroize();
486+
}
487+
}
488+
489+
/// Generic implementation of ZeroizeOnDrop for tuples up to 10 parameters.
490+
impl<A: ZeroizeOnDrop> ZeroizeOnDrop for (A,) {}
491+
492+
macro_rules! impl_zeroize_tuple {
493+
( $( $type_name:ident ),+ ) => {
494+
impl<$($type_name: Zeroize),+> Zeroize for ($($type_name),+) {
495+
fn zeroize(&mut self) {
496+
#[allow(non_snake_case)]
497+
let ($($type_name),+) = self;
498+
$($type_name.zeroize());+
499+
}
500+
}
501+
502+
impl<$($type_name: ZeroizeOnDrop),+> ZeroizeOnDrop for ($($type_name),+) { }
503+
}
504+
}
505+
506+
// Generic implementations for tuples up to 10 parameters.
507+
impl_zeroize_tuple!(A, B);
508+
impl_zeroize_tuple!(A, B, C);
509+
impl_zeroize_tuple!(A, B, C, D);
510+
impl_zeroize_tuple!(A, B, C, D, E);
511+
impl_zeroize_tuple!(A, B, C, D, E, F);
512+
impl_zeroize_tuple!(A, B, C, D, E, F, G);
513+
impl_zeroize_tuple!(A, B, C, D, E, F, G, H);
514+
impl_zeroize_tuple!(A, B, C, D, E, F, G, H, I);
515+
impl_zeroize_tuple!(A, B, C, D, E, F, G, H, I, J);
516+
466517
#[cfg(feature = "alloc")]
467518
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
468519
impl<Z> Zeroize for Vec<Z>
@@ -474,12 +525,12 @@ where
474525
/// Ensures the entire capacity of the `Vec` is zeroed. Cannot ensure that
475526
/// previous reallocations did not leave values on the heap.
476527
fn zeroize(&mut self) {
477-
use core::slice;
478528
// Zeroize all the initialized elements.
479529
self.iter_mut().zeroize();
480530

481531
// Set the Vec's length to 0 and drop all the elements.
482532
self.clear();
533+
483534
// Zero the full capacity of `Vec`.
484535
// Safety:
485536
//
@@ -490,6 +541,7 @@ where
490541
let uninit_slice = unsafe {
491542
slice::from_raw_parts_mut(self.as_mut_ptr() as *mut MaybeUninit<Z>, self.capacity())
492543
};
544+
493545
uninit_slice.zeroize();
494546
}
495547
}
@@ -530,17 +582,20 @@ impl Zeroize for CString {
530582
// mem::take uses replace internally to swap the pointer
531583
// Unfortunately this results in an allocation for a Box::new(&[0]) as CString must
532584
// contain a trailing zero byte
533-
let this = std::mem::take(self);
585+
let this = mem::take(self);
586+
534587
// - CString::into_bytes calls ::into_vec which takes ownership of the heap pointer
535588
// as a Vec<u8>
536589
// - Calling .zeroize() on the resulting vector clears out the bytes
537590
// From: https://github.com/RustCrypto/utils/pull/759#issuecomment-1087976570
538591
let mut buf = this.into_bytes();
539592
buf.zeroize();
593+
540594
// expect() should never fail, because zeroize() truncates the Vec
541595
let zeroed = CString::new(buf).expect("buf not truncated");
596+
542597
// Replace self by the zeroed CString to maintain the original ptr of the buffer
543-
let _ = std::mem::replace(self, zeroed);
598+
let _ = mem::replace(self, zeroed);
544599
}
545600
}
546601

@@ -685,6 +740,7 @@ unsafe fn volatile_set<T: Copy + Sized>(dst: *mut T, src: T, count: usize) {
685740
// allocation pointed to by `dst`, because `count <= isize::MAX` and because
686741
// `dst.add(count)` must not wrap around the address space.
687742
let ptr = dst.add(i);
743+
688744
// Safety:
689745
//
690746
// This is safe, because the pointer is valid and because `dst` is well aligned for `T` and
@@ -693,56 +749,31 @@ unsafe fn volatile_set<T: Copy + Sized>(dst: *mut T, src: T, count: usize) {
693749
}
694750
}
695751

696-
/// `PhantomData` is always zero sized so provide a Zeroize implementation.
697-
impl<Z> Zeroize for PhantomData<Z> {
698-
fn zeroize(&mut self) {}
699-
}
700-
/// `PhantomData` is always zero sized so provide a ZeroizeOnDrop implementation.
701-
impl<Z> ZeroizeOnDrop for PhantomData<Z> {}
702-
/// `PhantomPinned` is zero sized so provide a Zeroize implementation.
703-
impl Zeroize for PhantomPinned {
704-
fn zeroize(&mut self) {}
705-
}
706-
/// `PhantomPinned` is zero sized so provide a ZeroizeOnDrop implementation.
707-
impl ZeroizeOnDrop for PhantomPinned {}
708-
/// `()` is zero sized so provide a Zeroize implementation.
709-
impl Zeroize for () {
710-
fn zeroize(&mut self) {}
711-
}
712-
/// `()` is zero sized so provide a ZeroizeOnDrop implementation.
713-
impl ZeroizeOnDrop for () {}
752+
/// Internal module used as support for `AssertZeroizeOnDrop`.
753+
#[doc(hidden)]
754+
pub mod __internal {
755+
use super::*;
714756

715-
/// Generic implementation of Zeroize for tuples up to 10 parameters.
716-
impl<A: Zeroize> Zeroize for (A,) {
717-
fn zeroize(&mut self) {
718-
self.0.zeroize();
757+
/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
758+
pub trait AssertZeroizeOnDrop {
759+
fn zeroize_or_on_drop(self);
719760
}
720-
}
721-
/// Generic implementation of ZeroizeOnDrop for tuples up to 10 parameters.
722-
impl<A: ZeroizeOnDrop> ZeroizeOnDrop for (A,) {}
723-
macro_rules! impl_zeroize_tuple {
724-
( $( $type_name:ident )+ ) => {
725-
impl<$($type_name: Zeroize),+> Zeroize for ($($type_name),+) {
726-
fn zeroize(&mut self) {
727-
#[allow(non_snake_case)]
728-
let ($($type_name),+) = self;
729-
$($type_name.zeroize());+
730-
}
731-
}
732761

733-
impl<$($type_name: ZeroizeOnDrop),+> ZeroizeOnDrop for ($($type_name),+) { }
762+
impl<T: ZeroizeOnDrop + ?Sized> AssertZeroizeOnDrop for &&mut T {
763+
fn zeroize_or_on_drop(self) {}
764+
}
765+
766+
/// Auto-deref workaround for deriving `ZeroizeOnDrop`.
767+
pub trait AssertZeroize {
768+
fn zeroize_or_on_drop(&mut self);
769+
}
770+
771+
impl<T: Zeroize + ?Sized> AssertZeroize for T {
772+
fn zeroize_or_on_drop(&mut self) {
773+
self.zeroize()
774+
}
734775
}
735776
}
736-
// Generic implementations for tuples up to 10 parameters.
737-
impl_zeroize_tuple! { A B }
738-
impl_zeroize_tuple! { A B C }
739-
impl_zeroize_tuple! { A B C D }
740-
impl_zeroize_tuple! { A B C D E }
741-
impl_zeroize_tuple! { A B C D E F }
742-
impl_zeroize_tuple! { A B C D E F G }
743-
impl_zeroize_tuple! { A B C D E F G H }
744-
impl_zeroize_tuple! { A B C D E F G H I }
745-
impl_zeroize_tuple! { A B C D E F G H I L }
746777

747778
#[cfg(test)]
748779
mod tests {
@@ -784,9 +815,7 @@ mod tests {
784815
NonZeroI32,
785816
NonZeroI64,
786817
NonZeroI128,
787-
NonZeroIsize
788-
);
789-
non_zero_test!(
818+
NonZeroIsize,
790819
NonZeroU8,
791820
NonZeroU16,
792821
NonZeroU32,

0 commit comments

Comments
 (0)