Skip to content

Commit d1d7775

Browse files
committed
Improve worst-case behavior of CScript::FindAndDelete
Thanks to Sergio Lerner for identifying this issue and suggesting this kind of solution.
1 parent e2a30bc commit d1d7775

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/script/script.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,17 +570,26 @@ class CScript : public CScriptBase
570570
int nFound = 0;
571571
if (b.empty())
572572
return nFound;
573-
iterator pc = begin();
573+
CScript result;
574+
iterator pc = begin(), pc2 = begin();
574575
opcodetype opcode;
575576
do
576577
{
578+
result.insert(result.end(), pc2, pc);
577579
while (static_cast<size_t>(end() - pc) >= b.size() && std::equal(b.begin(), b.end(), pc))
578580
{
579-
pc = erase(pc, pc + b.size());
581+
pc = pc + b.size();
580582
++nFound;
581583
}
584+
pc2 = pc;
582585
}
583586
while (GetOp(pc, opcode));
587+
588+
if (nFound > 0) {
589+
result.insert(result.end(), pc2, end());
590+
*this = result;
591+
}
592+
584593
return nFound;
585594
}
586595
int Find(opcodetype op) const

0 commit comments

Comments
 (0)