Skip to content

Commit d5dddde

Browse files
MarcoFalkefurszy
MarcoFalke
authored andcommitted
[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 2e4ec58 commit d5dddde

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

src/test/test_bitcoin_fuzzy.cpp

+41-39
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,35 @@ enum TEST_ID {
4747
TEST_ID_END
4848
};
4949

50-
bool read_stdin(std::vector<uint8_t> &data) {
50+
static bool read_stdin(std::vector<uint8_t>& data)
51+
{
5152
uint8_t buffer[1024];
52-
ssize_t length=0;
53-
while((length = read(STDIN_FILENO, buffer, 1024)) > 0) {
54-
data.insert(data.end(), buffer, buffer+length);
53+
ssize_t length = 0;
54+
while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) {
55+
data.insert(data.end(), buffer, buffer + length);
5556

56-
if (data.size() > (1<<20)) return false;
57+
if (data.size() > (1 << 20)) return false;
5758
}
58-
return length==0;
59+
return length == 0;
5960
}
6061

61-
int test_one_input(std::vector<uint8_t> buffer) {
62-
if (buffer.size() < sizeof(uint32_t)) return 0;
62+
void test_one_input(std::vector<uint8_t> buffer)
63+
{
64+
if (buffer.size() < sizeof(uint32_t)) return;
6365

6466
uint32_t test_id = 0xffffffff;
6567
memcpy(&test_id, &buffer[0], sizeof(uint32_t));
6668
buffer.erase(buffer.begin(), buffer.begin() + sizeof(uint32_t));
6769

68-
if (test_id >= TEST_ID_END) return 0;
70+
if (test_id >= TEST_ID_END) return;
6971

7072
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
7173
try {
7274
int nVersion;
7375
ds >> nVersion;
7476
ds.SetVersion(nVersion);
7577
} catch (const std::ios_base::failure& e) {
76-
return 0;
78+
return;
7779
}
7880

7981
switch(test_id) {
@@ -83,15 +85,15 @@ int test_one_input(std::vector<uint8_t> buffer) {
8385
{
8486
CBlock block;
8587
ds >> block;
86-
} catch (const std::ios_base::failure& e) {return 0;}
88+
} catch (const std::ios_base::failure& e) {return;}
8789
break;
8890
}
8991
case CTRANSACTION_DESERIALIZE:
9092
{
9193
try
9294
{
9395
CTransaction tx(deserialize, ds);
94-
} catch (const std::ios_base::failure& e) {return 0;}
96+
} catch (const std::ios_base::failure& e) {return;}
9597
break;
9698
}
9799
case CBLOCKLOCATOR_DESERIALIZE:
@@ -100,7 +102,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
100102
{
101103
CBlockLocator bl;
102104
ds >> bl;
103-
} catch (const std::ios_base::failure& e) {return 0;}
105+
} catch (const std::ios_base::failure& e) {return;}
104106
break;
105107
}
106108
case CBLOCKMERKLEROOT:
@@ -111,7 +113,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
111113
ds >> block;
112114
bool mutated;
113115
BlockMerkleRoot(block, &mutated);
114-
} catch (const std::ios_base::failure& e) {return 0;}
116+
} catch (const std::ios_base::failure& e) {return;}
115117
break;
116118
}
117119
case CADDRMAN_DESERIALIZE:
@@ -120,7 +122,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
120122
{
121123
CAddrMan am;
122124
ds >> am;
123-
} catch (const std::ios_base::failure& e) {return 0;}
125+
} catch (const std::ios_base::failure& e) {return;}
124126
break;
125127
}
126128
case CBLOCKHEADER_DESERIALIZE:
@@ -129,7 +131,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
129131
{
130132
CBlockHeader bh;
131133
ds >> bh;
132-
} catch (const std::ios_base::failure& e) {return 0;}
134+
} catch (const std::ios_base::failure& e) {return;}
133135
break;
134136
}
135137
case CBANENTRY_DESERIALIZE:
@@ -138,7 +140,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
138140
{
139141
CBanEntry be;
140142
ds >> be;
141-
} catch (const std::ios_base::failure& e) {return 0;}
143+
} catch (const std::ios_base::failure& e) {return;}
142144
break;
143145
}
144146
case CTXUNDO_DESERIALIZE:
@@ -147,7 +149,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
147149
{
148150
CTxUndo tu;
149151
ds >> tu;
150-
} catch (const std::ios_base::failure& e) {return 0;}
152+
} catch (const std::ios_base::failure& e) {return;}
151153
break;
152154
}
153155
case CBLOCKUNDO_DESERIALIZE:
@@ -156,16 +158,16 @@ int test_one_input(std::vector<uint8_t> buffer) {
156158
{
157159
CBlockUndo bu;
158160
ds >> bu;
159-
} catch (const std::ios_base::failure& e) {return 0;}
161+
} catch (const std::ios_base::failure& e) {return;}
160162
break;
161163
}
162164
case CCOINS_DESERIALIZE:
163165
{
164166
try
165167
{
166-
Coin block;
167-
ds >> block;
168-
} catch (const std::ios_base::failure& e) {return 0;}
168+
Coin coin;
169+
ds >> coin;
170+
} catch (const std::ios_base::failure& e) {return;}
169171
break;
170172
}
171173
case CNETADDR_DESERIALIZE:
@@ -174,7 +176,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
174176
{
175177
CNetAddr na;
176178
ds >> na;
177-
} catch (const std::ios_base::failure& e) {return 0;}
179+
} catch (const std::ios_base::failure& e) {return;}
178180
break;
179181
}
180182
case CSERVICE_DESERIALIZE:
@@ -183,7 +185,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
183185
{
184186
CService s;
185187
ds >> s;
186-
} catch (const std::ios_base::failure& e) {return 0;}
188+
} catch (const std::ios_base::failure& e) {return;}
187189
break;
188190
}
189191
case CMESSAGEHEADER_DESERIALIZE:
@@ -193,8 +195,8 @@ int test_one_input(std::vector<uint8_t> buffer) {
193195
{
194196
CMessageHeader mh(pchMessageStart);
195197
ds >> mh;
196-
if (!mh.IsValid(pchMessageStart)) {return 0;}
197-
} catch (const std::ios_base::failure& e) {return 0;}
198+
if (!mh.IsValid(pchMessageStart)) {return;}
199+
} catch (const std::ios_base::failure& e) {return;}
198200
break;
199201
}
200202
case CADDRESS_DESERIALIZE:
@@ -203,7 +205,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
203205
{
204206
CAddress a;
205207
ds >> a;
206-
} catch (const std::ios_base::failure& e) {return 0;}
208+
} catch (const std::ios_base::failure& e) {return;}
207209
break;
208210
}
209211
case CINV_DESERIALIZE:
@@ -212,7 +214,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
212214
{
213215
CInv i;
214216
ds >> i;
215-
} catch (const std::ios_base::failure& e) {return 0;}
217+
} catch (const std::ios_base::failure& e) {return;}
216218
break;
217219
}
218220
case CBLOOMFILTER_DESERIALIZE:
@@ -221,7 +223,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
221223
{
222224
CBloomFilter bf;
223225
ds >> bf;
224-
} catch (const std::ios_base::failure& e) {return 0;}
226+
} catch (const std::ios_base::failure& e) {return;}
225227
break;
226228
}
227229
case CDISKBLOCKINDEX_DESERIALIZE:
@@ -230,7 +232,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
230232
{
231233
CDiskBlockIndex dbi;
232234
ds >> dbi;
233-
} catch (const std::ios_base::failure& e) {return 0;}
235+
} catch (const std::ios_base::failure& e) {return;}
234236
break;
235237
}
236238
case CTXOUTCOMPRESSOR_DESERIALIZE:
@@ -240,14 +242,14 @@ int test_one_input(std::vector<uint8_t> buffer) {
240242
try
241243
{
242244
ds >> toc;
243-
} catch (const std::ios_base::failure& e) {return 0;}
245+
} catch (const std::ios_base::failure& e) {return;}
244246

245247
break;
246248
}
247249
default:
248-
return 0;
250+
return;
249251
}
250-
return 0;
252+
return;
251253
}
252254

253255
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
@@ -257,13 +259,15 @@ void initialize() {
257259
}
258260

259261
// This function is used by libFuzzer
260-
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
262+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
263+
{
261264
test_one_input(std::vector<uint8_t>(data, data + size));
262265
return 0;
263266
}
264267

265268
// This function is used by libFuzzer
266-
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
269+
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
270+
{
267271
initialize();
268272
return 0;
269273
}
@@ -286,20 +290,18 @@ int main(int argc, char **argv)
286290
#ifdef __AFL_LOOP
287291
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
288292
// See fuzzing.md for details.
289-
int ret = 0;
290293
while (__AFL_LOOP(1000)) {
291294
std::vector<uint8_t> buffer;
292295
if (!read_stdin(buffer)) {
293296
continue;
294297
}
295-
ret = test_one_input(buffer);
298+
test_one_input(buffer);
296299
}
297-
return ret;
298300
#else
299301
std::vector<uint8_t> buffer;
300302
if (!read_stdin(buffer)) {
301303
return 0;
302304
}
303-
return test_one_input(buffer);
305+
test_one_input(buffer);
304306
#endif
305307
}

0 commit comments

Comments
 (0)