Skip to content

Comments

Add example showing UB for uninit Copy types in MaybeUninit::assume_init docs#153030

Open
bp7968h wants to merge 1 commit intorust-lang:mainfrom
bp7968h:docs-issue-150689
Open

Add example showing UB for uninit Copy types in MaybeUninit::assume_init docs#153030
bp7968h wants to merge 1 commit intorust-lang:mainfrom
bp7968h:docs-issue-150689

Conversation

@bp7968h
Copy link

@bp7968h bp7968h commented Feb 23, 2026

This PR adds an explicit example and explanation showing that calling assume_init() on uninitialized memory is undefined behavior even for simple types like integers that can hold any bit pattern.

Following up on the discussion in #150689 - waited for @Sekar-C-Mca to respond as suggested by @RalfJung.

Closes #150689

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 23, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 23, 2026

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet, scottmcm

@rustbot

This comment has been minimized.

/// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️
/// ```
///
/// This also applies to simple types that can hold any bit pattern, like integers, boolean and so on.

Choose a reason for hiding this comment

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

Booleans cannot hold any bit pattern.

Copy link
Author

Choose a reason for hiding this comment

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

Oh sorry my bad, a single bit, will amend that, any other improvements needed?

@joboet
Copy link
Member

joboet commented Feb 24, 2026

r? @RalfJung
How detailed do you think this should be? I'm sort of tempted to have this link to https://www.ralfj.de/blog/2019/07/14/uninit.html since it's just such a nice and concise explanation of why this is UB and the nasal demons that result from it.

@rustbot rustbot assigned RalfJung and unassigned joboet Feb 24, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 24, 2026

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docs for MaybeUninit::assume_init should indicate that reading uninit POD isn't sound

5 participants