@@ -54,33 +54,35 @@ enum TEST_ID {
54
54
TEST_ID_END
55
55
};
56
56
57
- static bool read_stdin (std::vector<uint8_t > &data) {
57
+ static bool read_stdin (std::vector<uint8_t >& data)
58
+ {
58
59
uint8_t buffer[1024 ];
59
- ssize_t length= 0 ;
60
- while ((length = read (STDIN_FILENO, buffer, 1024 )) > 0 ) {
61
- data.insert (data.end (), buffer, buffer+ length);
60
+ ssize_t length = 0 ;
61
+ while ((length = read (STDIN_FILENO, buffer, 1024 )) > 0 ) {
62
+ data.insert (data.end (), buffer, buffer + length);
62
63
63
- if (data.size () > (1 << 20 )) return false ;
64
+ if (data.size () > (1 << 20 )) return false ;
64
65
}
65
- return length== 0 ;
66
+ return length == 0 ;
66
67
}
67
68
68
- static int test_one_input (std::vector<uint8_t > buffer) {
69
- if (buffer.size () < sizeof (uint32_t )) return 0 ;
69
+ void test_one_input (std::vector<uint8_t > buffer)
70
+ {
71
+ if (buffer.size () < sizeof (uint32_t )) return ;
70
72
71
73
uint32_t test_id = 0xffffffff ;
72
74
memcpy (&test_id, buffer.data (), sizeof (uint32_t ));
73
75
buffer.erase (buffer.begin (), buffer.begin () + sizeof (uint32_t ));
74
76
75
- if (test_id >= TEST_ID_END) return 0 ;
77
+ if (test_id >= TEST_ID_END) return ;
76
78
77
79
CDataStream ds (buffer, SER_NETWORK, INIT_PROTO_VERSION);
78
80
try {
79
81
int nVersion;
80
82
ds >> nVersion;
81
83
ds.SetVersion (nVersion);
82
84
} catch (const std::ios_base::failure& e) {
83
- return 0 ;
85
+ return ;
84
86
}
85
87
86
88
switch (test_id) {
@@ -90,15 +92,15 @@ static int test_one_input(std::vector<uint8_t> buffer) {
90
92
{
91
93
CBlock block;
92
94
ds >> block;
93
- } catch (const std::ios_base::failure& e) {return 0 ;}
95
+ } catch (const std::ios_base::failure& e) {return ;}
94
96
break ;
95
97
}
96
98
case CTRANSACTION_DESERIALIZE:
97
99
{
98
100
try
99
101
{
100
102
CTransaction tx (deserialize, ds);
101
- } catch (const std::ios_base::failure& e) {return 0 ;}
103
+ } catch (const std::ios_base::failure& e) {return ;}
102
104
break ;
103
105
}
104
106
case CBLOCKLOCATOR_DESERIALIZE:
@@ -107,7 +109,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
107
109
{
108
110
CBlockLocator bl;
109
111
ds >> bl;
110
- } catch (const std::ios_base::failure& e) {return 0 ;}
112
+ } catch (const std::ios_base::failure& e) {return ;}
111
113
break ;
112
114
}
113
115
case CBLOCKMERKLEROOT:
@@ -118,7 +120,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
118
120
ds >> block;
119
121
bool mutated;
120
122
BlockMerkleRoot (block, &mutated);
121
- } catch (const std::ios_base::failure& e) {return 0 ;}
123
+ } catch (const std::ios_base::failure& e) {return ;}
122
124
break ;
123
125
}
124
126
case CADDRMAN_DESERIALIZE:
@@ -127,7 +129,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
127
129
{
128
130
CAddrMan am;
129
131
ds >> am;
130
- } catch (const std::ios_base::failure& e) {return 0 ;}
132
+ } catch (const std::ios_base::failure& e) {return ;}
131
133
break ;
132
134
}
133
135
case CBLOCKHEADER_DESERIALIZE:
@@ -136,7 +138,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
136
138
{
137
139
CBlockHeader bh;
138
140
ds >> bh;
139
- } catch (const std::ios_base::failure& e) {return 0 ;}
141
+ } catch (const std::ios_base::failure& e) {return ;}
140
142
break ;
141
143
}
142
144
case CBANENTRY_DESERIALIZE:
@@ -145,7 +147,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
145
147
{
146
148
CBanEntry be;
147
149
ds >> be;
148
- } catch (const std::ios_base::failure& e) {return 0 ;}
150
+ } catch (const std::ios_base::failure& e) {return ;}
149
151
break ;
150
152
}
151
153
case CTXUNDO_DESERIALIZE:
@@ -154,7 +156,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
154
156
{
155
157
CTxUndo tu;
156
158
ds >> tu;
157
- } catch (const std::ios_base::failure& e) {return 0 ;}
159
+ } catch (const std::ios_base::failure& e) {return ;}
158
160
break ;
159
161
}
160
162
case CBLOCKUNDO_DESERIALIZE:
@@ -163,7 +165,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
163
165
{
164
166
CBlockUndo bu;
165
167
ds >> bu;
166
- } catch (const std::ios_base::failure& e) {return 0 ;}
168
+ } catch (const std::ios_base::failure& e) {return ;}
167
169
break ;
168
170
}
169
171
case CCOINS_DESERIALIZE:
@@ -172,7 +174,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
172
174
{
173
175
Coin coin;
174
176
ds >> coin;
175
- } catch (const std::ios_base::failure& e) {return 0 ;}
177
+ } catch (const std::ios_base::failure& e) {return ;}
176
178
break ;
177
179
}
178
180
case CNETADDR_DESERIALIZE:
@@ -181,7 +183,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
181
183
{
182
184
CNetAddr na;
183
185
ds >> na;
184
- } catch (const std::ios_base::failure& e) {return 0 ;}
186
+ } catch (const std::ios_base::failure& e) {return ;}
185
187
break ;
186
188
}
187
189
case CSERVICE_DESERIALIZE:
@@ -190,7 +192,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
190
192
{
191
193
CService s;
192
194
ds >> s;
193
- } catch (const std::ios_base::failure& e) {return 0 ;}
195
+ } catch (const std::ios_base::failure& e) {return ;}
194
196
break ;
195
197
}
196
198
case CMESSAGEHEADER_DESERIALIZE:
@@ -200,8 +202,8 @@ static int test_one_input(std::vector<uint8_t> buffer) {
200
202
{
201
203
CMessageHeader mh (pchMessageStart);
202
204
ds >> mh;
203
- if (!mh.IsValid (pchMessageStart)) {return 0 ;}
204
- } catch (const std::ios_base::failure& e) {return 0 ;}
205
+ if (!mh.IsValid (pchMessageStart)) {return ;}
206
+ } catch (const std::ios_base::failure& e) {return ;}
205
207
break ;
206
208
}
207
209
case CADDRESS_DESERIALIZE:
@@ -210,7 +212,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
210
212
{
211
213
CAddress a;
212
214
ds >> a;
213
- } catch (const std::ios_base::failure& e) {return 0 ;}
215
+ } catch (const std::ios_base::failure& e) {return ;}
214
216
break ;
215
217
}
216
218
case CINV_DESERIALIZE:
@@ -219,7 +221,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
219
221
{
220
222
CInv i;
221
223
ds >> i;
222
- } catch (const std::ios_base::failure& e) {return 0 ;}
224
+ } catch (const std::ios_base::failure& e) {return ;}
223
225
break ;
224
226
}
225
227
case CBLOOMFILTER_DESERIALIZE:
@@ -228,7 +230,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
228
230
{
229
231
CBloomFilter bf;
230
232
ds >> bf;
231
- } catch (const std::ios_base::failure& e) {return 0 ;}
233
+ } catch (const std::ios_base::failure& e) {return ;}
232
234
break ;
233
235
}
234
236
case CDISKBLOCKINDEX_DESERIALIZE:
@@ -237,7 +239,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
237
239
{
238
240
CDiskBlockIndex dbi;
239
241
ds >> dbi;
240
- } catch (const std::ios_base::failure& e) {return 0 ;}
242
+ } catch (const std::ios_base::failure& e) {return ;}
241
243
break ;
242
244
}
243
245
case CTXOUTCOMPRESSOR_DESERIALIZE:
@@ -247,7 +249,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
247
249
try
248
250
{
249
251
ds >> toc;
250
- } catch (const std::ios_base::failure& e) {return 0 ;}
252
+ } catch (const std::ios_base::failure& e) {return ;}
251
253
252
254
break ;
253
255
}
@@ -257,7 +259,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
257
259
{
258
260
BlockTransactions bt;
259
261
ds >> bt;
260
- } catch (const std::ios_base::failure& e) {return 0 ;}
262
+ } catch (const std::ios_base::failure& e) {return ;}
261
263
262
264
break ;
263
265
}
@@ -267,29 +269,31 @@ static int test_one_input(std::vector<uint8_t> buffer) {
267
269
{
268
270
BlockTransactionsRequest btr;
269
271
ds >> btr;
270
- } catch (const std::ios_base::failure& e) {return 0 ;}
272
+ } catch (const std::ios_base::failure& e) {return ;}
271
273
272
274
break ;
273
275
}
274
276
default :
275
- return 0 ;
277
+ return ;
276
278
}
277
- return 0 ;
279
+ return ;
278
280
}
279
281
280
- static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
281
- void initialize () {
282
- globalVerifyHandle = MakeUnique<ECCVerifyHandle>();
282
+ void initialize ()
283
+ {
284
+ const static auto verify_handle = MakeUnique<ECCVerifyHandle>();
283
285
}
284
286
285
287
// This function is used by libFuzzer
286
- extern " C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) {
288
+ extern " C" int LLVMFuzzerTestOneInput (const uint8_t * data, size_t size)
289
+ {
287
290
test_one_input (std::vector<uint8_t >(data, data + size));
288
291
return 0 ;
289
292
}
290
293
291
294
// This function is used by libFuzzer
292
- extern " C" int LLVMFuzzerInitialize (int *argc, char ***argv) {
295
+ extern " C" int LLVMFuzzerInitialize (int * argc, char *** argv)
296
+ {
293
297
initialize ();
294
298
return 0 ;
295
299
}
@@ -312,20 +316,18 @@ int main(int argc, char **argv)
312
316
#ifdef __AFL_LOOP
313
317
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
314
318
// See fuzzing.md for details.
315
- int ret = 0 ;
316
319
while (__AFL_LOOP (1000 )) {
317
320
std::vector<uint8_t > buffer;
318
321
if (!read_stdin (buffer)) {
319
322
continue ;
320
323
}
321
- ret = test_one_input (buffer);
324
+ test_one_input (buffer);
322
325
}
323
- return ret;
324
326
#else
325
327
std::vector<uint8_t > buffer;
326
328
if (!read_stdin (buffer)) {
327
329
return 0 ;
328
330
}
329
- return test_one_input (buffer);
331
+ test_one_input (buffer);
330
332
#endif
331
333
}
0 commit comments