-
Notifications
You must be signed in to change notification settings - Fork 4
/
0007-alg_ds.rs
82 lines (72 loc) · 2.17 KB
/
0007-alg_ds.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*!
```rudra-poc
[target]
crate = "alg_ds"
version = "0.3.1"
[test]
cargo_flags = ["--release"]
[report]
issue_url = "https://gitlab.com/dvshapkin/alg-ds/-/issues/1"
issue_date = 2020-08-25
rustsec_url = "https://github.com/RustSec/advisory-db/pull/362"
rustsec_id = "RUSTSEC-2020-0033"
[[bugs]]
analyzer = "Manual"
guide = "UnsafeDestructor"
bug_class = "Other"
rudra_report_locations = []
[[bugs]]
analyzer = "UnsafeDataflow"
guide = "Manual"
bug_class = "Other"
rudra_report_locations = ["src/ds/matrix.rs:97:5: 104:6"]
```
!*/
#![forbid(unsafe_code)]
use alg_ds::ds::matrix::Matrix;
use std::sync::atomic::{AtomicUsize, Ordering};
static creation_cnt: AtomicUsize = AtomicUsize::new(0);
static drop_cnt: AtomicUsize = AtomicUsize::new(0);
#[derive(Clone)]
struct DropDetector(u32);
impl Default for DropDetector {
fn default() -> Self {
creation_cnt.fetch_add(1, Ordering::Relaxed);
DropDetector(12345)
}
}
impl Drop for DropDetector {
fn drop(&mut self) {
drop_cnt.fetch_add(1, Ordering::Relaxed);
println!("Dropping {}", self.0);
}
}
fn main() {
// Please check along with the code snippets above.
{
// `*ptr = value` acts by dropping existing contents at `ptr`.
// `Matrix::fill_with()` uses this pattern which result in dropping
// uninitialized, unallocated struct.
//
// Note that the creation of a mutable reference to uninitialized memory
// region is already UB by itself.
// `ptr::write` and `MaybeUninit` should be used for the initialization.
let _ = Matrix::<DropDetector>::new(1, 1);
}
{
// (Bonus) Integer overflow in `layout()` allows to create a huge matrix.
// Fortunately, every access to the internal buffer are bound-checked,
// so this doesn't lead to obvious UB by itself.
let mat = Matrix::<usize>::new(15326306685794188004, 0x123456789);
println!(
"rows: {}, cols: {}, number of elements: {}",
mat.rows(),
mat.cols(),
mat.elements_number()
);
}
assert_eq!(
creation_cnt.load(Ordering::Relaxed),
drop_cnt.load(Ordering::Relaxed)
);
}