Skip to content

Commit 19032c7

Browse files
committed
Merge #18612: script: Remove undocumented and unused operator+
ccccd51 script: Remove undocumented and unused operator+ (MarcoFalke) Pull request description: This operator has no documented use case and is also unused outside of test code. The test code and all other (imaginary) code that might use this operator is written more clear and concise by the existing CScript push operators for opcodes and data. Removing the operator is also going to protect against accidentally reintroducing bugs like this bitcoin/bitcoin@6ff5f71#diff-8458adcedc17d046942185cb709ff5c3L1135 (last time it was used). ACKs for top commit: laanwj: ACK ccccd51 Tree-SHA512: 43898ac77e4d9643d9f8ac6f8f65497a4f0bbb1fb5dcaecc839c3719aa36181ba77befb213e59a9f33a20a29e0173a0e9c4763b1930940b32c3d1598b3e39af9
2 parents c90a9e6 + ccccd51 commit 19032c7

File tree

3 files changed

+24
-61
lines changed

3 files changed

+24
-61
lines changed

src/script/script.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -419,28 +419,15 @@ class CScript : public CScriptBase
419419
READWRITEAS(CScriptBase, *this);
420420
}
421421

422-
CScript& operator+=(const CScript& b)
423-
{
424-
reserve(size() + b.size());
425-
insert(end(), b.begin(), b.end());
426-
return *this;
427-
}
428-
429-
friend CScript operator+(const CScript& a, const CScript& b)
430-
{
431-
CScript ret = a;
432-
ret += b;
433-
return ret;
434-
}
435-
436422
explicit CScript(int64_t b) { operator<<(b); }
437-
438423
explicit CScript(opcodetype b) { operator<<(b); }
439424
explicit CScript(const CScriptNum& b) { operator<<(b); }
440425
// delete non-existent constructor to defend against future introduction
441426
// e.g. via prevector
442427
explicit CScript(const std::vector<unsigned char>& b) = delete;
443428

429+
/** Delete non-existent operator to defend against future introduction */
430+
CScript& operator<<(const CScript& b) = delete;
444431

445432
CScript& operator<<(int64_t b) { return push_int64(b); }
446433

@@ -487,15 +474,6 @@ class CScript : public CScriptBase
487474
return *this;
488475
}
489476

490-
CScript& operator<<(const CScript& b)
491-
{
492-
// I'm not sure if this should push the script or concatenate scripts.
493-
// If there's ever a use for pushing a script onto a script, delete this member fn
494-
assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
495-
return *this;
496-
}
497-
498-
499477
bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
500478
{
501479
return GetScriptOp(pc, end(), opcodeRet, &vchRet);
@@ -506,7 +484,6 @@ class CScript : public CScriptBase
506484
return GetScriptOp(pc, end(), opcodeRet, nullptr);
507485
}
508486

509-
510487
/** Encode/decode small integers: */
511488
static int DecodeOP_N(opcodetype opcode)
512489
{

src/test/fuzz/script_ops.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ void test_one_input(const std::vector<uint8_t>& buffer)
1717
CScript script = ConsumeScript(fuzzed_data_provider);
1818
while (fuzzed_data_provider.remaining_bytes() > 0) {
1919
switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 7)) {
20-
case 0:
21-
script += ConsumeScript(fuzzed_data_provider);
20+
case 0: {
21+
CScript s = ConsumeScript(fuzzed_data_provider);
22+
script = std::move(s);
2223
break;
23-
case 1:
24-
script = script + ConsumeScript(fuzzed_data_provider);
24+
}
25+
case 1: {
26+
const CScript& s = ConsumeScript(fuzzed_data_provider);
27+
script = s;
2528
break;
29+
}
2630
case 2:
2731
script << fuzzed_data_provider.ConsumeIntegral<int64_t>();
2832
break;

src/test/script_tests.cpp

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ struct KeyData
217217

218218
KeyData()
219219
{
220-
221220
key0.Set(vchKey0, vchKey0 + 32, false);
222221
key0C.Set(vchKey0, vchKey0 + 32, true);
223222
pubkey0 = key0.GetPubKey();
@@ -272,9 +271,9 @@ class TestBuilder
272271

273272
void DoPush(const std::vector<unsigned char>& data)
274273
{
275-
DoPush();
276-
push = data;
277-
havePush = true;
274+
DoPush();
275+
push = data;
276+
havePush = true;
278277
}
279278

280279
public:
@@ -306,10 +305,10 @@ class TestBuilder
306305
return *this;
307306
}
308307

309-
TestBuilder& Add(const CScript& _script)
308+
TestBuilder& Opcode(const opcodetype& _op)
310309
{
311310
DoPush();
312-
spendTx.vin[0].scriptSig += _script;
311+
spendTx.vin[0].scriptSig << _op;
313312
return *this;
314313
}
315314

@@ -326,8 +325,9 @@ class TestBuilder
326325
return *this;
327326
}
328327

329-
TestBuilder& Push(const CScript& _script) {
330-
DoPush(std::vector<unsigned char>(_script.begin(), _script.end()));
328+
TestBuilder& Push(const CScript& _script)
329+
{
330+
DoPush(std::vector<unsigned char>(_script.begin(), _script.end()));
331331
return *this;
332332
}
333333

@@ -681,22 +681,22 @@ BOOST_AUTO_TEST_CASE(script_build)
681681

682682
tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
683683
"2-of-2 with two identical keys and sigs pushed using OP_DUP but no SIGPUSHONLY", 0
684-
).Num(0).PushSig(keys.key1).Add(CScript() << OP_DUP));
684+
).Num(0).PushSig(keys.key1).Opcode(OP_DUP));
685685
tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
686686
"2-of-2 with two identical keys and sigs pushed using OP_DUP", SCRIPT_VERIFY_SIGPUSHONLY
687-
).Num(0).PushSig(keys.key1).Add(CScript() << OP_DUP).ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
687+
).Num(0).PushSig(keys.key1).Opcode(OP_DUP).ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
688688
tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
689689
"P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0, true
690-
).PushSig(keys.key2).Add(CScript() << OP_NOP8).PushRedeem());
690+
).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem());
691691
tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
692692
"P2PK with non-push scriptSig but with P2SH validation", 0
693-
).PushSig(keys.key2).Add(CScript() << OP_NOP8));
693+
).PushSig(keys.key2).Opcode(OP_NOP8));
694694
tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
695695
"P2SH(P2PK) with non-push scriptSig but no SIGPUSHONLY", SCRIPT_VERIFY_P2SH, true
696-
).PushSig(keys.key2).Add(CScript() << OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
696+
).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
697697
tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
698698
"P2SH(P2PK) with non-push scriptSig but not P2SH", SCRIPT_VERIFY_SIGPUSHONLY, true
699-
).PushSig(keys.key2).Add(CScript() << OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
699+
).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
700700
tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
701701
"2-of-2 with two identical keys and sigs pushed", SCRIPT_VERIFY_SIGPUSHONLY
702702
).Num(0).PushSig(keys.key1).PushSig(keys.key1));
@@ -1470,24 +1470,6 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps)
14701470
BOOST_CHECK(!script.HasValidOps());
14711471
}
14721472

1473-
BOOST_AUTO_TEST_CASE(script_can_append_self)
1474-
{
1475-
CScript s, d;
1476-
1477-
s = ScriptFromHex("00");
1478-
s += s;
1479-
d = ScriptFromHex("0000");
1480-
BOOST_CHECK(s == d);
1481-
1482-
// check doubling a script that's large enough to require reallocation
1483-
static const char hex[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f";
1484-
s = CScript() << ParseHex(hex) << OP_CHECKSIG;
1485-
d = CScript() << ParseHex(hex) << OP_CHECKSIG << ParseHex(hex) << OP_CHECKSIG;
1486-
s += s;
1487-
BOOST_CHECK(s == d);
1488-
}
1489-
1490-
14911473
#if defined(HAVE_CONSENSUS_LIB)
14921474

14931475
/* Test simple (successful) usage of bitcoinconsensus_verify_script */

0 commit comments

Comments
 (0)