Skip to content

Commit 2fcd9cc

Browse files
committed
scripted-diff: Use randbits/bool instead of randrange where possible
-BEGIN VERIFY SCRIPT- sed -i 's/insecure_randbits(1)/insecure_randbool()/g' src/test/*_tests.cpp sed -i 's/insecure_randrange(2)/insecure_randbool()/g' src/test/*_tests.cpp sed -i 's/insecure_randrange(4)/insecure_randbits(2)/g' src/test/*_tests.cpp sed -i 's/insecure_randrange(32)/insecure_randbits(5)/g' src/test/*_tests.cpp sed -i 's/insecure_randrange(256)/insecure_randbits(8)/g' src/test/*_tests.cpp -END VERIFY SCRIPT-
1 parent 2ada678 commit 2fcd9cc

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/test/coins_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CCoinsViewTest : public CCoinsView
4343
return false;
4444
}
4545
coin = it->second;
46-
if (coin.IsSpent() && insecure_randrange(2) == 0) {
46+
if (coin.IsSpent() && insecure_randbool() == 0) {
4747
// Randomly return false in case of an empty entry.
4848
return false;
4949
}
@@ -200,20 +200,20 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
200200

201201
if (insecure_randrange(100) == 0) {
202202
// Every 100 iterations, flush an intermediate cache
203-
if (stack.size() > 1 && insecure_randrange(2) == 0) {
203+
if (stack.size() > 1 && insecure_randbool() == 0) {
204204
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
205205
stack[flushIndex]->Flush();
206206
}
207207
}
208208
if (insecure_randrange(100) == 0) {
209209
// Every 100 iterations, change the cache stack.
210-
if (stack.size() > 0 && insecure_randrange(2) == 0) {
210+
if (stack.size() > 0 && insecure_randbool() == 0) {
211211
//Remove the top cache
212212
stack.back()->Flush();
213213
delete stack.back();
214214
stack.pop_back();
215215
}
216-
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) {
216+
if (stack.size() == 0 || (stack.size() < 4 && insecure_randbool())) {
217217
//Add a new cache
218218
CCoinsView* tip = &base;
219219
if (stack.size() > 0) {
@@ -433,19 +433,19 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
433433

434434
if (insecure_randrange(100) == 0) {
435435
// Every 100 iterations, flush an intermediate cache
436-
if (stack.size() > 1 && insecure_randrange(2) == 0) {
436+
if (stack.size() > 1 && insecure_randbool() == 0) {
437437
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
438438
stack[flushIndex]->Flush();
439439
}
440440
}
441441
if (insecure_randrange(100) == 0) {
442442
// Every 100 iterations, change the cache stack.
443-
if (stack.size() > 0 && insecure_randrange(2) == 0) {
443+
if (stack.size() > 0 && insecure_randbool() == 0) {
444444
stack.back()->Flush();
445445
delete stack.back();
446446
stack.pop_back();
447447
}
448-
if (stack.size() == 0 || (stack.size() < 4 && insecure_randrange(2))) {
448+
if (stack.size() == 0 || (stack.size() < 4 && insecure_randbool())) {
449449
CCoinsView* tip = &base;
450450
if (stack.size() > 0) {
451451
tip = stack.back();

src/test/pmt_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree
2222
// flip one bit in one of the hashes - this should break the authentication
2323
void Damage() {
2424
unsigned int n = insecure_randrange(vHash.size());
25-
int bit = insecure_randrange(256);
25+
int bit = insecure_randbits(8);
2626
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
2727
}
2828
};

src/test/prevector_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
209209
test.resize(new_size);
210210
}
211211
if (insecure_randbits(3) == 3) {
212-
test.insert(insecure_randrange(test.size() + 1), 1 + insecure_randrange(2), insecure_rand());
212+
test.insert(insecure_randrange(test.size() + 1), 1 + insecure_randbool(), insecure_rand());
213213
}
214214
if (insecure_randbits(3) == 4) {
215-
int del = std::min<int>(test.size(), 1 + (insecure_randrange(2)));
215+
int del = std::min<int>(test.size(), 1 + (insecure_randbool()));
216216
int beg = insecure_randrange(test.size() + 1 - del);
217217
test.erase(beg, beg + del);
218218
}
@@ -224,19 +224,19 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
224224
}
225225
if (insecure_randbits(5) == 7) {
226226
int values[4];
227-
int num = 1 + (insecure_randrange(4));
227+
int num = 1 + (insecure_randbits(2));
228228
for (int k = 0; k < num; k++) {
229229
values[k] = insecure_rand();
230230
}
231231
test.insert_range(insecure_randrange(test.size() + 1), values, values + num);
232232
}
233233
if (insecure_randbits(5) == 8) {
234-
int del = std::min<int>(test.size(), 1 + (insecure_randrange(4)));
234+
int del = std::min<int>(test.size(), 1 + (insecure_randbits(2)));
235235
int beg = insecure_randrange(test.size() + 1 - del);
236236
test.erase(beg, beg + del);
237237
}
238238
if (insecure_randbits(5) == 9) {
239-
test.reserve(insecure_randrange(32));
239+
test.reserve(insecure_randbits(5));
240240
}
241241
if (insecure_randbits(6) == 10) {
242242
test.shrink_to_fit();
@@ -248,7 +248,7 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
248248
test.clear();
249249
}
250250
if (insecure_randbits(9) == 12) {
251-
test.assign(insecure_randrange(32), insecure_rand());
251+
test.assign(insecure_randbits(5), insecure_rand());
252252
}
253253
if (insecure_randbits(3) == 3) {
254254
test.swap();

src/test/sighash_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
9898
tx.nVersion = insecure_rand();
9999
tx.vin.clear();
100100
tx.vout.clear();
101-
tx.nLockTime = (insecure_randrange(2)) ? insecure_rand() : 0;
102-
int ins = (insecure_randrange(4)) + 1;
103-
int outs = fSingle ? ins : (insecure_randrange(4)) + 1;
101+
tx.nLockTime = (insecure_randbool()) ? insecure_rand() : 0;
102+
int ins = (insecure_randbits(2)) + 1;
103+
int outs = fSingle ? ins : (insecure_randbits(2)) + 1;
104104
for (int in = 0; in < ins; in++) {
105105
tx.vin.push_back(CTxIn());
106106
CTxIn &txin = tx.vin.back();
107107
txin.prevout.hash = insecure_rand256();
108-
txin.prevout.n = insecure_randrange(4);
108+
txin.prevout.n = insecure_randbits(2);
109109
RandomScript(txin.scriptSig);
110-
txin.nSequence = (insecure_randrange(2)) ? insecure_rand() : (unsigned int)-1;
110+
txin.nSequence = (insecure_randbool()) ? insecure_rand() : (unsigned int)-1;
111111
}
112112
for (int out = 0; out < outs; out++) {
113113
tx.vout.push_back(CTxOut());

0 commit comments

Comments
 (0)