Repository navigation
Add a zeroize feature to erase SmallVec contents - #748
Merged
Merged
Conversation
alejandro-vaz
requested changes
Oct 8, 2026
alejandro-vaz
approved these changes
Oct 8, 2026
Collaborator
|
thanks @GauravPawar101 |
Contributor
Author
|
Welcome |
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.
This adds an optional
zeroizefeature that implementszeroize::ZeroizeforSmallVec. Calling it wipes the live elements and the whole backing buffer (inline or heap), then empties the vector. Think of it asclear, except the freed slots get erased too, so secrets don't stay around in memory.How it works
Same order
zeroizeuses forVec:self.spare_capacity_mut().zeroize()wipes the unused tail first, while the length is still intact.self.iter_mut().zeroize()wipes the live elements in place. This way, things like aString's heap buffer are erased before the element is dropped.self.clear()drops the wiped elements and resets the length.The spare capacity has to go before
clear. Afterclear, the spare view covers the entire allocation, so wiping it would hit the elements a second time. Doing it in this order means every byte is written exactly once, and no unsafe code is needed.Things to know
zeroizeand the allocation isn't thrown away.no_std.zeroizeis optional (default-features = false), and without the feature nothing changes.ZeroizeOnDrop, since that would change drop semantics.Tests
Added
tests/zeroize.rs, which covers:CI is green: fmt, clippy, docs, markdownlint, tests on stable, nightly and MSRV 1.86, the
thumbv7m-none-eabino_std build, and Miri with all features.Closes #746