Skip to content

Commit fab4bed

Browse files
author
MarcoFalke
committed
[test] fuzz: make test_one_input return void
The return value is always 0 and not used, so might as well return void
1 parent d14ef57 commit fab4bed

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

src/test/test_bitcoin_fuzzy.cpp

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,35 @@ enum TEST_ID {
5454
TEST_ID_END
5555
};
5656

57-
static bool read_stdin(std::vector<uint8_t> &data) {
57+
static bool read_stdin(std::vector<uint8_t>& data)
58+
{
5859
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);
6263

63-
if (data.size() > (1<<20)) return false;
64+
if (data.size() > (1 << 20)) return false;
6465
}
65-
return length==0;
66+
return length == 0;
6667
}
6768

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;
7072

7173
uint32_t test_id = 0xffffffff;
7274
memcpy(&test_id, buffer.data(), sizeof(uint32_t));
7375
buffer.erase(buffer.begin(), buffer.begin() + sizeof(uint32_t));
7476

75-
if (test_id >= TEST_ID_END) return 0;
77+
if (test_id >= TEST_ID_END) return;
7678

7779
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
7880
try {
7981
int nVersion;
8082
ds >> nVersion;
8183
ds.SetVersion(nVersion);
8284
} catch (const std::ios_base::failure& e) {
83-
return 0;
85+
return;
8486
}
8587

8688
switch(test_id) {
@@ -90,15 +92,15 @@ static int test_one_input(std::vector<uint8_t> buffer) {
9092
{
9193
CBlock block;
9294
ds >> block;
93-
} catch (const std::ios_base::failure& e) {return 0;}
95+
} catch (const std::ios_base::failure& e) {return;}
9496
break;
9597
}
9698
case CTRANSACTION_DESERIALIZE:
9799
{
98100
try
99101
{
100102
CTransaction tx(deserialize, ds);
101-
} catch (const std::ios_base::failure& e) {return 0;}
103+
} catch (const std::ios_base::failure& e) {return;}
102104
break;
103105
}
104106
case CBLOCKLOCATOR_DESERIALIZE:
@@ -107,7 +109,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
107109
{
108110
CBlockLocator bl;
109111
ds >> bl;
110-
} catch (const std::ios_base::failure& e) {return 0;}
112+
} catch (const std::ios_base::failure& e) {return;}
111113
break;
112114
}
113115
case CBLOCKMERKLEROOT:
@@ -118,7 +120,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
118120
ds >> block;
119121
bool mutated;
120122
BlockMerkleRoot(block, &mutated);
121-
} catch (const std::ios_base::failure& e) {return 0;}
123+
} catch (const std::ios_base::failure& e) {return;}
122124
break;
123125
}
124126
case CADDRMAN_DESERIALIZE:
@@ -127,7 +129,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
127129
{
128130
CAddrMan am;
129131
ds >> am;
130-
} catch (const std::ios_base::failure& e) {return 0;}
132+
} catch (const std::ios_base::failure& e) {return;}
131133
break;
132134
}
133135
case CBLOCKHEADER_DESERIALIZE:
@@ -136,7 +138,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
136138
{
137139
CBlockHeader bh;
138140
ds >> bh;
139-
} catch (const std::ios_base::failure& e) {return 0;}
141+
} catch (const std::ios_base::failure& e) {return;}
140142
break;
141143
}
142144
case CBANENTRY_DESERIALIZE:
@@ -145,7 +147,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
145147
{
146148
CBanEntry be;
147149
ds >> be;
148-
} catch (const std::ios_base::failure& e) {return 0;}
150+
} catch (const std::ios_base::failure& e) {return;}
149151
break;
150152
}
151153
case CTXUNDO_DESERIALIZE:
@@ -154,7 +156,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
154156
{
155157
CTxUndo tu;
156158
ds >> tu;
157-
} catch (const std::ios_base::failure& e) {return 0;}
159+
} catch (const std::ios_base::failure& e) {return;}
158160
break;
159161
}
160162
case CBLOCKUNDO_DESERIALIZE:
@@ -163,7 +165,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
163165
{
164166
CBlockUndo bu;
165167
ds >> bu;
166-
} catch (const std::ios_base::failure& e) {return 0;}
168+
} catch (const std::ios_base::failure& e) {return;}
167169
break;
168170
}
169171
case CCOINS_DESERIALIZE:
@@ -172,7 +174,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
172174
{
173175
Coin coin;
174176
ds >> coin;
175-
} catch (const std::ios_base::failure& e) {return 0;}
177+
} catch (const std::ios_base::failure& e) {return;}
176178
break;
177179
}
178180
case CNETADDR_DESERIALIZE:
@@ -181,7 +183,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
181183
{
182184
CNetAddr na;
183185
ds >> na;
184-
} catch (const std::ios_base::failure& e) {return 0;}
186+
} catch (const std::ios_base::failure& e) {return;}
185187
break;
186188
}
187189
case CSERVICE_DESERIALIZE:
@@ -190,7 +192,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
190192
{
191193
CService s;
192194
ds >> s;
193-
} catch (const std::ios_base::failure& e) {return 0;}
195+
} catch (const std::ios_base::failure& e) {return;}
194196
break;
195197
}
196198
case CMESSAGEHEADER_DESERIALIZE:
@@ -200,8 +202,8 @@ static int test_one_input(std::vector<uint8_t> buffer) {
200202
{
201203
CMessageHeader mh(pchMessageStart);
202204
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;}
205207
break;
206208
}
207209
case CADDRESS_DESERIALIZE:
@@ -210,7 +212,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
210212
{
211213
CAddress a;
212214
ds >> a;
213-
} catch (const std::ios_base::failure& e) {return 0;}
215+
} catch (const std::ios_base::failure& e) {return;}
214216
break;
215217
}
216218
case CINV_DESERIALIZE:
@@ -219,7 +221,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
219221
{
220222
CInv i;
221223
ds >> i;
222-
} catch (const std::ios_base::failure& e) {return 0;}
224+
} catch (const std::ios_base::failure& e) {return;}
223225
break;
224226
}
225227
case CBLOOMFILTER_DESERIALIZE:
@@ -228,7 +230,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
228230
{
229231
CBloomFilter bf;
230232
ds >> bf;
231-
} catch (const std::ios_base::failure& e) {return 0;}
233+
} catch (const std::ios_base::failure& e) {return;}
232234
break;
233235
}
234236
case CDISKBLOCKINDEX_DESERIALIZE:
@@ -237,7 +239,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
237239
{
238240
CDiskBlockIndex dbi;
239241
ds >> dbi;
240-
} catch (const std::ios_base::failure& e) {return 0;}
242+
} catch (const std::ios_base::failure& e) {return;}
241243
break;
242244
}
243245
case CTXOUTCOMPRESSOR_DESERIALIZE:
@@ -247,7 +249,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
247249
try
248250
{
249251
ds >> toc;
250-
} catch (const std::ios_base::failure& e) {return 0;}
252+
} catch (const std::ios_base::failure& e) {return;}
251253

252254
break;
253255
}
@@ -257,7 +259,7 @@ static int test_one_input(std::vector<uint8_t> buffer) {
257259
{
258260
BlockTransactions bt;
259261
ds >> bt;
260-
} catch (const std::ios_base::failure& e) {return 0;}
262+
} catch (const std::ios_base::failure& e) {return;}
261263

262264
break;
263265
}
@@ -267,29 +269,31 @@ static int test_one_input(std::vector<uint8_t> buffer) {
267269
{
268270
BlockTransactionsRequest btr;
269271
ds >> btr;
270-
} catch (const std::ios_base::failure& e) {return 0;}
272+
} catch (const std::ios_base::failure& e) {return;}
271273

272274
break;
273275
}
274276
default:
275-
return 0;
277+
return;
276278
}
277-
return 0;
279+
return;
278280
}
279281

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>();
283285
}
284286

285287
// 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+
{
287290
test_one_input(std::vector<uint8_t>(data, data + size));
288291
return 0;
289292
}
290293

291294
// 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+
{
293297
initialize();
294298
return 0;
295299
}
@@ -312,20 +316,18 @@ int main(int argc, char **argv)
312316
#ifdef __AFL_LOOP
313317
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
314318
// See fuzzing.md for details.
315-
int ret = 0;
316319
while (__AFL_LOOP(1000)) {
317320
std::vector<uint8_t> buffer;
318321
if (!read_stdin(buffer)) {
319322
continue;
320323
}
321-
ret = test_one_input(buffer);
324+
test_one_input(buffer);
322325
}
323-
return ret;
324326
#else
325327
std::vector<uint8_t> buffer;
326328
if (!read_stdin(buffer)) {
327329
return 0;
328330
}
329-
return test_one_input(buffer);
331+
test_one_input(buffer);
330332
#endif
331333
}

0 commit comments

Comments
 (0)