Skip to content

hardening: mark append_fixed_slice_as_bytes as unsafe and document safety requirements#20

Open
MegaRedHand wants to merge 2 commits into
mainfrom
hardening/unsafe-safety-comments
Open

hardening: mark append_fixed_slice_as_bytes as unsafe and document safety requirements#20
MegaRedHand wants to merge 2 commits into
mainfrom
hardening/unsafe-safety-comments

Conversation

@MegaRedHand

@MegaRedHand MegaRedHand commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR marks append_fixed_slice_as_bytes as unsafe and documents the safety requirements for both remaining unsafe blocks in the SSZ encode/decode paths.

append_fixed_slice_as_bytes reinterprets &[T] as &[u8] via from_raw_parts. This is only sound if T has no padding bytes, because the Reference allows padding within a valid T to hold uninitialized bytes, and a &[u8] covering uninitialized bytes violates the requirement that a reference must point to valid values.

decode_byte_array_vec uses copy_nonoverlapping + set_len to bulk-decode Vec<[u8; N]>. Since [u8; N] is trivially valid for any bit pattern, no safety contract is needed on the function itself; the // SAFETY: comment documents the bounds check.

Changes

File Function Change
encode.rs append_fixed_slice_as_bytes Marked unsafe fn, added # Safety doc ("T must not have padding"), added // SAFETY: at each call site
decode.rs decode_byte_array_vec Added // SAFETY: comment documenting the bounds invariant

decode_byte_array_vec: drop "no padding" (not a soundness requirement
for the decode direction; [u8; N] is valid for any bit pattern).

append_fixed_slice_as_bytes: keep "no padding" (the encode direction
reads T as bytes, and the Reference allows padding in valid T values
to hold uninitialized bytes, making the u8 reinterpretation UB).
@MegaRedHand MegaRedHand changed the title hardening: add safety comments to unsafe blocks hardening: mark append_fixed_slice_as_bytes as unsafe and document safety requirements Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants