diff --git a/zeroize/src/lib.rs b/zeroize/src/lib.rs index d780af55..45fd42ca 100644 --- a/zeroize/src/lib.rs +++ b/zeroize/src/lib.rs @@ -526,14 +526,17 @@ where /// Ensures the entire capacity of the `Vec` is zeroed. Cannot ensure that /// previous reallocations did not leave values on the heap. fn zeroize(&mut self) { + // Zero the spare (uninitialized) capacity first, i.e. everything + // beyond `len`. This must happen before `clear()` resets `len` to 0, + // otherwise `spare_capacity_mut()` would cover the whole allocation + // and re-zero the initialized elements a second time below. + self.spare_capacity_mut().zeroize(); + // Zeroize all the initialized elements. self.iter_mut().zeroize(); - // Set the Vec's length to 0 and drop all the elements. + // Set the Vec's length to 0 and drop all the (already-zeroized) elements. self.clear(); - - // Zero the full capacity of `Vec`. - self.spare_capacity_mut().zeroize(); } }