Skip to content

Fix TryFromBytes::try_read_from* - #3712

Open
jswrenn wants to merge 1 commit into
Gd1b460a8ec2e8aa8adf2e070c329d9f209c7c89cfrom
G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e
Open

jswrenn wants to merge 1 commit into
Gd1b460a8ec2e8aa8adf2e070c329d9f209c7c89cfrom
G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e

Conversation

@jswrenn

@jswrenn jswrenn commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Refactors these routines to not depend on padding byte preservation
across moves. Along the way, we make the try_read_from helper safe,
generic, and better-optimized. Instruction counts for the
try_read_from_* change by -36% to -55%.


⬇️ Download this PR

Branch

git fetch origin refs/heads/G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e && git checkout -b pr-G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e FETCH_HEAD

Checkout

git fetch origin refs/heads/G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e && git checkout FETCH_HEAD

Cherry Pick

git fetch origin refs/heads/G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e && git cherry-pick FETCH_HEAD

Pull

git pull origin refs/heads/G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e

Stacked PRs enabled by GHerrit.

Refactors these routines to not depend on padding byte preservation
across moves. Along the way, we make the `try_read_from` helper safe,
generic, and better-optimized. Instruction counts for the
`try_read_from_*` change by -36% to -55%.

gherrit-pr-id: G35c8b8db33f4c984d5716af9a26c0a5ec9bd9c7e
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.63%. Comparing base (714c822) to head (12bc48f).

Additional details and impacted files
@@                              Coverage Diff                              @@
##           Gd1b460a8ec2e8aa8adf2e070c329d9f209c7c89c    #3712      +/-   ##
=============================================================================
+ Coverage                                      92.51%   92.63%   +0.11%     
=============================================================================
  Files                                             20       20              
  Lines                                           6362     6424      +62     
=============================================================================
+ Hits                                            5886     5951      +65     
+ Misses                                           476      473       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 12bc48fa9b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread zerocopy/src/lib.rs
Comment on lines +3546 to +3550
// SAFETY: The size check ensures that `bytes` is readable for
// `size_of::<T>()` bytes. `candidate.as_mut_ptr()` is writable for the
// same number of bytes because `MaybeUninit<T>` has `T`'s size. Both
// pointers are non-null and aligned for `u8`, including when `T` is
// zero-sized. The fresh local allocation cannot overlap `source`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Cite the byte-copy safety premises

This new unsafe block relies on uncited premises that source and the local allocation cannot overlap and that a u8 copy preserves initialization (and provenance for typed sources containing pointers). The quoted copy_nonoverlapping excerpt only states that bytes are copied and requires non-overlap; it does not substantiate those premises or even the stated read, write, and alignment requirements. Extend the proof with versioned official quotations covering these guarantees, or use an operation such as ptr::copy that removes the unproven non-overlap premise. zerocopy/agent_docs/unsafe_code.mdL54-L65

Useful? React with 👍 / 👎.

Comment thread zerocopy/src/lib.rs
Comment on lines +3546 to +3550
// SAFETY: The size check ensures that `bytes` is readable for
// `size_of::<T>()` bytes. `candidate.as_mut_ptr()` is writable for the
// same number of bytes because `MaybeUninit<T>` has `T`'s size. Both
// pointers are non-null and aligned for `u8`, including when `T` is
// zero-sized. The fresh local allocation cannot overlap `source`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need to cite that this is bit-valid for the destination (MaybeUninit<T>) type?

This branch has not been deployed

No deployments
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.

3 participants