Skip to content

Add a zeroize feature to erase SmallVec contents - #1

Closed
GauravPawar101 wants to merge 2 commits into
v2from
feat/zeroize-impl
Closed

GauravPawar101 wants to merge 2 commits into
v2from
feat/zeroize-impl

Conversation

@GauravPawar101

Copy link
Copy Markdown
Owner

Summary

Introduces an optional zeroize feature that implements zeroize::Zeroize
for SmallVec. Calling zeroize wipes every element and the entire backing
allocation, inline or heap, then empties the collection. It behaves like
clear, but also erases the freed slots so stale secret data cannot linger
in memory.

Implementation

The wipe order mirrors what zeroize does 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, so nested
    allocations such as a String's heap buffer are erased before the
    elements are dropped.
  3. self.clear() drops the wiped elements and resets the length.

Wiping the spare capacity before clear is what keeps the pass single-write:
after clear, the spare view covers the entire allocation and would wipe the
elements a second time. Every byte of the allocation is written exactly once,
and no unsafe code is added.

Notes

  • When spilled, the stored heap pointer and capacity metadata are left
    untouched, so the vector stays usable after zeroize and its allocation is
    not wasted.
  • The feature is no_std compatible. zeroize is optional with
    default-features = false, and without the feature there is no behavior
    change.
  • ZeroizeOnDrop is intentionally not implemented; it would change drop
    semantics.

Tests

New tests/zeroize.rs covers 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-eabi no_std target, and Miri with all
features.

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.

1 participant