Skip to content

Commit

Permalink
Improve worst-case behavior of CScript::FindAndDelete
Browse files Browse the repository at this point in the history
Thanks to Sergio Lerner for identifying this issue and suggesting this kind of solution.
  • Loading branch information
pstratem committed Apr 22, 2016
1 parent e2a30bc commit d1d7775
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,17 +570,26 @@ class CScript : public CScriptBase
int nFound = 0;
if (b.empty())
return nFound;
iterator pc = begin();
CScript result;
iterator pc = begin(), pc2 = begin();
opcodetype opcode;
do
{
result.insert(result.end(), pc2, pc);
while (static_cast<size_t>(end() - pc) >= b.size() && std::equal(b.begin(), b.end(), pc))
{
pc = erase(pc, pc + b.size());
pc = pc + b.size();
++nFound;
}
pc2 = pc;
}
while (GetOp(pc, opcode));

if (nFound > 0) {
result.insert(result.end(), pc2, end());
*this = result;
}

return nFound;
}
int Find(opcodetype op) const
Expand Down

0 comments on commit d1d7775

Please sign in to comment.