Skip to content

Add a zeroize feature to erase SmallVec contents - #748

Merged
alejandro-vaz merged 2 commits into
servo:v2from
GauravPawar101:feat/zeroize-impl
Oct 8, 2026
Merged

alejandro-vaz merged 2 commits into
servo:v2from
GauravPawar101:feat/zeroize-impl

Conversation

@GauravPawar101

@GauravPawar101 GauravPawar101 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

This adds an optional zeroize feature that implements zeroize::Zeroize for SmallVec. Calling it wipes the live elements and the whole backing buffer (inline or heap), then empties the vector. Think of it as clear, except the freed slots get erased too, so secrets don't stay around in memory.

How it works

Same order zeroize uses for Vec:

  1. self.spare_capacity_mut().zeroize() wipes the unused tail first, while the length is still intact.
  2. self.iter_mut().zeroize() wipes the live elements in place. This way, things like a String's heap buffer are erased before the element is dropped.
  3. self.clear() drops the wiped elements and resets the length.

The spare capacity has to go before clear. After clear, 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

  • If the vector has spilled to the heap, I leave the pointer and capacity alone. It stays usable after zeroize and the allocation isn't thrown away.
  • Works with no_std. zeroize is optional (default-features = false), and without the feature nothing changes.
  • I didn't implement ZeroizeOnDrop, since that would change drop semantics.

Tests

Added tests/zeroize.rs, which covers:

  • inline storage (buffer zeroed, capacity kept, still reusable)
  • spilled storage (heap metadata preserved, full allocation zeroed)
  • an empty vector with spare capacity (stale bytes wiped)

CI is green: fmt, clippy, docs, markdownlint, tests on stable, nightly and MSRV 1.86, the thumbv7m-none-eabi no_std build, and Miri with all features.

Closes #746

Comment thread src/lib.rs Outdated
Comment thread Cargo.toml Outdated
@alejandro-vaz

Copy link
Copy Markdown
Collaborator

thanks @GauravPawar101

@GauravPawar101

Copy link
Copy Markdown
Contributor Author

Welcome

@alejandro-vaz
alejandro-vaz added this pull request to the merge queue Oct 8, 2026
Merged via the queue into servo:v2 with commit ec61a13 Oct 8, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement zeroize::Zeroize for SmallVec

2 participants