Repository navigation
Add a zeroize feature to erase SmallVec contents - #1
Closed
GauravPawar101 wants to merge 2 commits into
Closed
GauravPawar101 wants to merge 2 commits into
GauravPawar101 wants to merge 2 commits into
Conversation
GauravPawar101
force-pushed
the
feat/zeroize-impl
branch
from
October 8, 2026 05:37
5494a76 to
47c8901
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduces an optional
zeroizefeature that implementszeroize::Zeroizefor
SmallVec. Callingzeroizewipes every element and the entire backingallocation, inline or heap, then empties the collection. It behaves like
clear, but also erases the freed slots so stale secret data cannot lingerin memory.
Implementation
The wipe order mirrors what
zeroizedoes forVec:self.spare_capacity_mut().zeroize()wipes the unused tail first, whilethe length is still intact.
self.iter_mut().zeroize()wipes the live elements in place, so nestedallocations such as a
String's heap buffer are erased before theelements are dropped.
self.clear()drops the wiped elements and resets the length.Wiping the spare capacity before
clearis what keeps the pass single-write:after
clear, the spare view covers the entire allocation and would wipe theelements a second time. Every byte of the allocation is written exactly once,
and no unsafe code is added.
Notes
untouched, so the vector stays usable after
zeroizeand its allocation isnot wasted.
zeroizeis optional withdefault-features = false, and without the feature there is no behaviorchange.
ZeroizeOnDropis intentionally not implemented; it would change dropsemantics.
Tests
New
tests/zeroize.rscovers inline storage (buffer zeroed, capacity kept,still reusable), spilled storage (heap metadata preserved, full allocation
zeroed), and an empty vector with capacity (wiped stale bytes).
All checks pass: fmt, clippy, docs, markdownlint, tests on stable, nightly,
MSRV 1.86, the
thumbv7m-none-eabino_std target, and Miri with allfeatures.