1// RUN: %check_clang_tidy %s cert-msc51-cpp %t -- \
2// RUN: -config="{CheckOptions: {cert-msc51-cpp.DisallowedSeedTypes: 'some_type,time_t'}}"
3
4namespace std {
5
6void srand(int seed);
7
8template <class UIntType, UIntType a, UIntType c, UIntType m>
9struct linear_congruential_engine {
10 linear_congruential_engine(int _ = 0);
11 void seed(int _ = 0);
12};
13using default_random_engine = linear_congruential_engine<unsigned int, 1, 2, 3>;
14
15using size_t = int;
16template <class UIntType, size_t w, size_t n, size_t m, size_t r,
17 UIntType a, size_t u, UIntType d, size_t s,
18 UIntType b, size_t t,
19 UIntType c, size_t l, UIntType f>
20struct mersenne_twister_engine {
21 mersenne_twister_engine(int _ = 0);
22 void seed(int _ = 0);
23};
24using mt19937 = mersenne_twister_engine<unsigned int, 32, 624, 397, 21, 0x9908b0df, 11, 0xffffffff, 7, 0x9d2c5680, 15, 0xefc60000, 18, 1812433253>;
25
26template <class UIntType, size_t w, size_t s, size_t r>
27struct subtract_with_carry_engine {
28 subtract_with_carry_engine(int _ = 0);
29 void seed(int _ = 0);
30};
31using ranlux24_base = subtract_with_carry_engine<unsigned int, 24, 10, 24>;
32
33template <class Engine, size_t p, size_t r>
34struct discard_block_engine {
35 discard_block_engine();
36 discard_block_engine(int _);
37 void seed();
38 void seed(int _);
39};
40using ranlux24 = discard_block_engine<ranlux24_base, 223, 23>;
41
42template <class Engine, size_t w, class UIntType>
43struct independent_bits_engine {
44 independent_bits_engine();
45 independent_bits_engine(int _);
46 void seed();
47 void seed(int _);
48};
49using independent_bits = independent_bits_engine<ranlux24_base, 223, int>;
50
51template <class Engine, size_t k>
52struct shuffle_order_engine {
53 shuffle_order_engine();
54 shuffle_order_engine(int _);
55 void seed();
56 void seed(int _);
57};
58using shuffle_order = shuffle_order_engine<ranlux24_base, 223>;
59
60struct random_device {
61 random_device();
62 int operator()();
63};
64} // namespace std
65
66using time_t = unsigned int;
67time_t time(time_t *t);
68
69void f() {
70 const int seed = 2;
71 time_t t;
72
73 std::srand(seed: 0);
74 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
75 std::srand(seed);
76 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
77 std::srand(seed: time(t: &t));
78 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
79
80 // One instantiation for every engine
81 std::default_random_engine engine1;
82 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
83 std::default_random_engine engine2(1);
84 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
85 std::default_random_engine engine3(seed);
86 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
87 std::default_random_engine engine4(time(t: &t));
88 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
89 engine1.seed();
90 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
91 engine1.seed(1);
92 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
93 engine1.seed(seed);
94 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
95 engine1.seed(time(t: &t));
96 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
97
98 std::mt19937 engine5;
99 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
100 std::mt19937 engine6(1);
101 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
102 std::mt19937 engine7(seed);
103 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
104 std::mt19937 engine8(time(t: &t));
105 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
106 engine5.seed();
107 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
108 engine5.seed(1);
109 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
110 engine5.seed(seed);
111 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
112 engine5.seed(time(t: &t));
113 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
114
115 std::ranlux24_base engine9;
116 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
117 std::ranlux24_base engine10(1);
118 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
119 std::ranlux24_base engine11(seed);
120 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
121 std::ranlux24_base engine12(time(t: &t));
122 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
123 engine9.seed();
124 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
125 engine9.seed(1);
126 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
127 engine9.seed(seed);
128 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
129 engine9.seed(time(t: &t));
130 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
131
132 std::ranlux24 engine13;
133 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
134 std::ranlux24 engine14(1);
135 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
136 std::ranlux24 engine15(seed);
137 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
138 std::ranlux24 engine16(time(t: &t));
139 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
140 engine13.seed();
141 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
142 engine13.seed(1);
143 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
144 engine13.seed(seed);
145 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
146 engine13.seed(time(t: &t));
147 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
148
149 std::independent_bits engine17;
150 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
151 std::independent_bits engine18(1);
152 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
153 std::independent_bits engine19(seed);
154 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
155 std::independent_bits engine20(time(t: &t));
156 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
157 engine17.seed();
158 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
159 engine17.seed(1);
160 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
161 engine17.seed(seed);
162 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
163 engine17.seed(time(t: &t));
164 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
165
166 std::shuffle_order engine21;
167 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
168 std::shuffle_order engine22(1);
169 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
170 std::shuffle_order engine23(seed);
171 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
172 std::shuffle_order engine24(time(t: &t));
173 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
174 engine21.seed();
175 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a default argument will generate a predictable sequence of values [cert-msc51-cpp]
176 engine21.seed(1);
177 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
178 engine21.seed(seed);
179 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a constant value will generate a predictable sequence of values [cert-msc51-cpp]
180 engine21.seed(time(t: &t));
181 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: random number generator seeded with a disallowed source of seed value will generate a predictable sequence of values [cert-msc51-cpp]
182}
183
184struct A {
185 A(int _ = 0);
186 void seed(int _ = 0);
187};
188
189void g() {
190 int n = 1;
191 std::default_random_engine engine1(n);
192 std::mt19937 engine2(n);
193 std::ranlux24_base engine3(n);
194 std::ranlux24 engine4(n);
195 std::independent_bits engine5(n);
196 std::shuffle_order engine6(n);
197
198 std::random_device dev;
199 std::default_random_engine engine7(dev());
200 std::mt19937 engine8(dev());
201 std::ranlux24_base engine9(dev());
202 std::ranlux24 engine10(dev());
203 std::independent_bits engine11(dev());
204 std::shuffle_order engine12(dev());
205
206 A a1;
207 A a2(1);
208 a1.seed();
209 a1.seed(1);
210 a1.seed(n);
211}
212

source code of clang-tools-extra/test/clang-tidy/checkers/cert/msc51-cpp.cpp