Skip to content

octets: remove unsafe from integer read/write macros - #2868

Merged
ghedo merged 1 commit into
cloudflare:masterfrom
b84330808:octets-forbid-unsafe
Nov 12, 2026
Merged

ghedo merged 1 commit into
cloudflare:masterfrom
b84330808:octets-forbid-unsafe

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

peek_u!/get_u! and put_u! copy the big-endian bytes through raw pointers. This switches them to from_be_bytes()/to_be_bytes() on a fixed-size array. The length is checked before the copy and is a constant at each call site, so the bounds checks are optimized out: the release assembly of the octets crate (cargo rustc -p octets --release -- --emit asm) has identical instructions before and after; only panic-location line numbers differ.

With these two blocks gone the crate has no unsafe code left, so it now has #![forbid(unsafe_code)], in line with AGENTS.md (unsafe only at FFI boundaries).

Checked with cargo test -p octets --all-features, clippy -D warnings, cargo +nightly fmt --check, and the test_invalid_len_compilation_fail cfg still fails with the two static_assert errors.

peek_u!/get_u! and put_u! copied the big-endian bytes with raw pointer
arithmetic. Use from_be_bytes()/to_be_bytes() on a fixed-size array
instead. The length is already checked before the copy and is a
compile-time constant, so the generated code is unchanged.

With this the crate has no unsafe code left, so forbid it.
@b84330808
b84330808 requested a review from a team as a code owner November 4, 2026 16:07
@ghedo ghedo added type: refactor Restructures code without an intended behavior change. crate: octets Changes related to octets. labels Nov 11, 2026
@ghedo
ghedo enabled auto-merge (rebase) November 12, 2026 16:48
@ghedo
ghedo merged commit 42ec116 into cloudflare:master Nov 12, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crate: octets Changes related to octets. type: refactor Restructures code without an intended behavior change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants