Skip to content

#235 [Coding Guideline]: Do not read uninitialized memory of any non-union type as a typed value#240

Open
manhatsu wants to merge 29 commits intorustfoundation:mainfrom
manhatsu:doc/no-uninit-value
Open

#235 [Coding Guideline]: Do not read uninitialized memory of any non-union type as a typed value#240
manhatsu wants to merge 29 commits intorustfoundation:mainfrom
manhatsu:doc/no-uninit-value

Conversation

@manhatsu
Copy link
Contributor

@manhatsu manhatsu commented Dec 4, 2025

Closes #235.

@netlify
Copy link

netlify bot commented Dec 4, 2025

Deploy Preview for scrc-coding-guidelines ready!

Name Link
🔨 Latest commit f84f500
🔍 Latest deploy log https://app.netlify.com/projects/scrc-coding-guidelines/deploys/69713bd31d74a30008064502
😎 Deploy Preview https://deploy-preview-240--scrc-coding-guidelines.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@rcseacord
Copy link
Collaborator

I've made some improvement suggestions in a PR here: manhatsu#1

@manhatsu
Copy link
Contributor Author

manhatsu commented Dec 4, 2025

I've made some improvement suggestions in a PR here: manhatsu#1

Thank you very much. Merged to this branch

@PLeVasseur
Copy link
Collaborator

Hey @manhatsu 👋 it looks like from the CI that a new tag needs to be added.

Could you follow what @rcseacord did in this PR to add the unsafe tag with an appropriate description? Ideally you would do that as a separate PR, as that's easy to review and merge.

Copy link
Collaborator

@PLeVasseur PLeVasseur left a comment

Choose a reason for hiding this comment

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

Hi @manhatsu -- thank you for contributing. Please see the comment I left on how to generate a template.

Copy link

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

What @inkreasing says is correct. This description is insufficient to reflect the restrictions imposed by MIRI here:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=abb9da1c391902b21c03ed1d21767b58

Note this is not UB before line 15.

Bytes remain uninit until written. You may not read uninitialized bytes as any initialized type, period, not even if "all" bitpatterns are considered valid, because uninit is the 257th bitpattern for a byte, effectively: 0xUU. By contrast, u8 is 0x00 through 0xFF, inclusive. We use MaybeUninit<u8> to indicate the final state is possible, and it is valid to read that value (well, from any allocation that has a byte in it, at least).

@manhatsu manhatsu force-pushed the doc/no-uninit-value branch from 9b81ff4 to 0e2776c Compare December 8, 2025 00:44
Copy link
Collaborator

@felix91gr felix91gr left a comment

Choose a reason for hiding this comment

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

I hope this helps

Copy link

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

Much better.

@felix91gr
Copy link
Collaborator

Sidenote: this probably should be part of the Unsafety chapter. Anything dealing with upholding validity invariants in unsafe should probably go there, I believe.

@rcseacord
Copy link
Collaborator

Sidenote: this probably should be part of the Unsafety chapter. Anything dealing with upholding validity invariants in unsafe should probably go there, I believe.

I created an issue #241 that discusses this. See what you think. Anyway, we should probably have this discussion there.

@PLeVasseur
Copy link
Collaborator

Hi @manhatsu, @rcseacord -- please see this PR: #288

Please simply replace the current commits on your feature branch with that single commit on the above PR.

That way we can keep the review history on this PR.

@rcseacord
Copy link
Collaborator

@felix91gr I might be coming around to your view that union should be split out into a different rule.

rcseacord and others added 6 commits January 19, 2026 13:42
Clarified guideline on reading uninitialized memory, specifying non-union types and adding citations for better understanding.
Add miri directives and normalize citations/bibliography entries so the guideline builds cleanly.
Allow warnings for UB demonstrations and prefix unused locals in the uninitialized-memory guideline to keep example tests clean.
rcseacord and others added 3 commits January 20, 2026 04:21
Copy link
Collaborator

@felix91gr felix91gr left a comment

Choose a reason for hiding this comment

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

Sorry I took so long. I hope this helps.

I think the examples are pretty good in general; I'm mostly suggesting improvements to the presentation plus a couple of details here and there.

:tags: undefined-behavior, unsafe

Do not read uninitialized memory of any non-union type as a typed value :cite:`gui_uyp3mCj77FS8:RUSTNOMICON-UNINIT`.
This is sometimes referred to as *transmuting* or *read-at-type*.
Copy link
Collaborator

@felix91gr felix91gr Jan 21, 2026

Choose a reason for hiding this comment

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

What would you say is the difference between this and std::mem::transmute? Maybe we ought to point out that this is not the same, since transmute is quite the famous unsafe function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@felix91gr I got this information from @workingjubilee
I believe these things are roughly equivalent in the context they are used here in that they are all typed reads.

Comment on lines 26 to 30
* ``assume_init_drop``
* ``assume_init_mut``
* ``assume_init_read``
* ``assume_init_ref``
* ``array_assume_init``
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps these APIs should also get linked here, much like assume_init is above

Copy link
Collaborator

Choose a reason for hiding this comment

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

added some links, now we'll see if it builds.

:status: draft

This noncompliant example creates a reference from uninitialized memory.
Creating a reference from arbitrary or uninitialized bytes is undefined behavior :cite:`gui_uyp3mCj77FS8:RUST-REF-BEHAVIOR`.
Copy link
Collaborator

@felix91gr felix91gr Jan 21, 2026

Choose a reason for hiding this comment

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

I'm not sure this is entirely correct. A reference could be created from arbitrary bytes, e.g.

let bytes = 999u32.to_be_bytes();
let nine_nine_nine = u32::from_be_bytes(bytes);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't find this any longer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you mean? :O

You can't find the sentence that says "Creating a reference from arbitrary or uninitialized bytes is undefined behavior"?

It should be at line 119

Copy link
Collaborator

@rcseacord rcseacord left a comment

Choose a reason for hiding this comment

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

lgtm

@felix91gr felix91gr changed the title #235 [Coding Guideline]: Do not create values from uninitialized memory #235 [Coding Guideline]: Do not read uninitialized memory of any non-union type as a typed value Jan 21, 2026
@felix91gr
Copy link
Collaborator

Upkeep: I've updated the title of the PR so that it may be the same as the title of the guideline

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

Labels

chapter: values coding guideline An issue related to a suggestion for a coding guideline

Development

Successfully merging this pull request may close these issues.

[Coding Guideline]: Do not create values from uninitialized memory

6 participants