question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unitialized memory

See original GitHub issue

Under what conditions is it valid to use any of these?

  • let x: T = std::mem::uninitialized(); on the stack
  • Box::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:closed
  • Created 6 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
eddybcommented, Jul 15, 2017

@SimonSapin Instead of XOR one could also use x != x or x == x.

0reactions
RalfJungcommented, Nov 29, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found