From a9a04e81b0c1e62e1554dfc89806955c98fcd7d8 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 18 Sep 2017 11:30:01 +0900 Subject: [PATCH] Regex: Ensure we only ever have a single thread on a given instruction --- src/regex_impl.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/regex_impl.cc b/src/regex_impl.cc index 6fc513d7..1378fcd2 100644 --- a/src/regex_impl.cc +++ b/src/regex_impl.cc @@ -311,10 +311,13 @@ struct ThreadedExecutor return { StepResult::Consumed, inst }; case RegexProgram::Jump: inst = m_program.begin() + *reinterpret_cast(inst); + // if instruction is already going to be executed, drop this thread + if (std::find(m_threads.begin(), m_threads.end(), inst) != m_threads.end()) + return { StepResult::Failed }; break; case RegexProgram::Split: { - m_threads.push_back(m_program.begin() + *reinterpret_cast(inst)); + add_thread(*reinterpret_cast(inst)); inst += sizeof(RegexProgram::Offset); break; } @@ -362,6 +365,13 @@ struct ThreadedExecutor return false; } + void add_thread(RegexProgram::Offset pos) + { + const char* inst = m_program.begin() + pos; + if (std::find(m_threads.begin(), m_threads.end(), inst) == m_threads.end()) + m_threads.push_back(inst); + } + bool is_line_start() const { return m_pos == m_subject.begin() or *(m_pos-1) == '\n';