Repository navigation
octets: remove unsafe from integer read/write macros - #2868
Merged
Merged
Conversation
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.
ghedo
approved these changes
Nov 10, 2026
ghedo
enabled auto-merge (rebase)
November 12, 2026 16:48
inamdarminaz
approved these changes
Nov 12, 2026
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.
peek_u!/get_u!andput_u!copy the big-endian bytes through raw pointers. This switches them tofrom_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 thetest_invalid_len_compilation_failcfg still fails with the two static_assert errors.