blob: c069b9adf2d966cc4c7592f5ef27574c84257be4 (
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#![warn(clippy::uninit_vec)]
use std::mem::MaybeUninit;
#[derive(Default)]
struct MyVec {
vec: Vec<u8>,
}
union MyOwnMaybeUninit {
value: u8,
uninit: (),
}
fn main() {
// with_capacity() -> set_len() should be detected
let mut vec: Vec<u8> = Vec::with_capacity(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates uninitial
unsafe {
vec.set_len(200);
}
// reserve() -> set_len() should be detected
vec.reserve(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates uninitial
unsafe {
vec.set_len(200);
}
// new() -> set_len() should be detected
let mut vec: Vec<u8> = Vec::new();
//~^ ERROR: calling `set_len()` on empty `Vec` creates out-of-bound values
unsafe {
vec.set_len(200);
}
// default() -> set_len() should be detected
let mut vec: Vec<u8> = Default::default();
//~^ ERROR: calling `set_len()` on empty `Vec` creates out-of-bound values
unsafe {
vec.set_len(200);
}
let mut vec: Vec<u8> = Vec::default();
//~^ ERROR: calling `set_len()` on empty `Vec` creates out-of-bound values
unsafe {
vec.set_len(200);
}
// test when both calls are enclosed in the same unsafe block
unsafe {
let mut vec: Vec<u8> = Vec::with_capacity(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates unini
vec.set_len(200);
vec.reserve(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates unini
vec.set_len(200);
}
let mut vec: Vec<u8> = Vec::with_capacity(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates uninitial
unsafe {
// test the case where there are other statements in the following unsafe block
vec.set_len(200);
assert!(vec.len() == 200);
}
// handle vec stored in the field of a struct
let mut my_vec = MyVec::default();
my_vec.vec.reserve(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates uninitial
unsafe {
my_vec.vec.set_len(200);
}
my_vec.vec = Vec::with_capacity(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates uninitial
unsafe {
my_vec.vec.set_len(200);
}
// Test `#[allow(...)]` attributes on inner unsafe block (shouldn't trigger)
let mut vec: Vec<u8> = Vec::with_capacity(1000);
#[allow(clippy::uninit_vec)]
unsafe {
vec.set_len(200);
}
// MaybeUninit-wrapped types should not be detected
unsafe {
let mut vec: Vec<MaybeUninit<u8>> = Vec::with_capacity(1000);
vec.set_len(200);
let mut vec: Vec<(MaybeUninit<u8>, MaybeUninit<bool>)> = Vec::with_capacity(1000);
vec.set_len(200);
let mut vec: Vec<(MaybeUninit<u8>, [MaybeUninit<bool>; 2])> = Vec::with_capacity(1000);
vec.set_len(200);
}
// known false negative
let mut vec1: Vec<u8> = Vec::with_capacity(1000);
let mut vec2: Vec<u8> = Vec::with_capacity(1000);
unsafe {
vec1.set_len(200);
vec2.set_len(200);
}
// set_len(0) should not be detected
let mut vec: Vec<u8> = Vec::with_capacity(1000);
unsafe {
vec.set_len(0);
}
// ZSTs should not be detected
let mut vec: Vec<()> = Vec::with_capacity(1000);
unsafe {
vec.set_len(10);
}
// unions should not be detected
let mut vec: Vec<MyOwnMaybeUninit> = Vec::with_capacity(1000);
unsafe {
vec.set_len(10);
}
polymorphic::<()>();
fn polymorphic<T>() {
// We are conservative around polymorphic types.
let mut vec: Vec<T> = Vec::with_capacity(1000);
//~^ ERROR: calling `set_len()` immediately after reserving a buffer creates unini
unsafe {
vec.set_len(10);
}
}
fn poly_maybe_uninit<T>() {
// We are conservative around polymorphic types.
let mut vec: Vec<MaybeUninit<T>> = Vec::with_capacity(1000);
unsafe {
vec.set_len(10);
}
}
}
|