Unitialized memory
See original GitHub issueUnder what conditions is it valid to use any of these?
let x: T = std::mem::uninitialized();
on the stackBox::new(std::mem::uninitialized())
- The part of
Vec
between.len()
and.capacity()
- The memory pointed to by
alloc::heap::allocate()
without first writing to it - (Possibly many other similar cases…)
Let’s assume !Drop
(or that we use std::mem::forget
and are being very careful about panic-safety), and types (like u32
) for which all bit patterns are valid.
Reading uninitialized memory is Bad and should be avoided, but what’s the worst that could happen? Undefined values might fine in many cases. Or is it Undefined Behavior of the “the optimizer is allowed to eat your lunch and elide half your program” kind?
As a concrete example, consider reallocate
which when copying reads from the source pointer. That data is not necessarily entirely initialized: Vec::with_capacity(10).reserve(100)
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Solving Uninitialized Stack Memory on Windows
Uninitialized Memory Disclosure – Uninitialized memory is copied across a trust boundary and its contents are disclosed to a less privileged ...
Read more >c++ - Why am I getting the error "using uninitialized memory 'i ...
In the body of the while loop while (i < 10000000000000000) { long long i = i*i; cout << i ; }. you...
Read more >Uninitialized Memory Access - Intel
Uninitialized Memory Access ... Occurs when a read of an uninitialized memory location is reported. ... on these buffers. ... Initialize the offending ......
Read more >Uninitialized Memory - The Rustonomicon
All runtime-allocated memory in a Rust program begins its life as uninitialized. In this state the value of the memory is an indeterminate...
Read more >Reading uninitialized memory
Reading uninitialized memory ... If you attempt to read or write to memory that was previously allocated, the result will be a conflict...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@SimonSapin Instead of XOR one could also use
x != x
orx == x
.The story of
mem::uninitialized
has progressed quite a lot in the last years (and that function has been deprecated and declared to be basically impossible to use correctly), so I will close this.