blob: 0bb483668abc10f2c4e2094a2758754b5675f5d9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
### What it does
Checks for `mem::replace(&mut _, mem::uninitialized())`
and `mem::replace(&mut _, mem::zeroed())`.
### Why is this bad?
This will lead to undefined behavior even if the
value is overwritten later, because the uninitialized value may be
observed in the case of a panic.
### Example
```
use std::mem;
#[allow(deprecated, invalid_value)]
fn myfunc (v: &mut Vec<i32>) {
let taken_v = unsafe { mem::replace(v, mem::uninitialized()) };
let new_v = may_panic(taken_v); // undefined behavior on panic
mem::forget(mem::replace(v, new_v));
}
```
The [take_mut](https://docs.rs/take_mut) crate offers a sound solution,
at the cost of either lazily creating a replacement value or aborting
on panic, to ensure that the uninitialized value cannot be observed.
|