Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal/catch_textflow.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2021-11-04 00:22:50 +0100
committerMartin Hořeňovský2021-11-04 00:23:56 +0100
commit912df7df354fd6c814b5d7c45ac44f494a5fb5fe (patch)
tree04cc730476213dde1b17aede165cb9ffef62690f /src/catch2/internal/catch_textflow.cpp
parent931f41b4d62bb572216c958899258e6d0b592f25 (diff)
downloadcatch3-912df7df354fd6c814b5d7c45ac44f494a5fb5fe.tar.gz
Fix quadratic runtime when linebreaking strings without newlines
The problem was that every line would iterate from current line start position to the end of the string, looking for a newline to break on, leading to accidentally quadratic runtime. With this change, the code only ever searches up to the current line's length and not more. Credit to @jorgenpt for the fix suggestion. Closes #2315
Diffstat (limited to 'src/catch2/internal/catch_textflow.cpp')
-rw-r--r--src/catch2/internal/catch_textflow.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/catch2/internal/catch_textflow.cpp b/src/catch2/internal/catch_textflow.cpp
index f8b8e7f4..993e1622 100644
--- a/src/catch2/internal/catch_textflow.cpp
+++ b/src/catch2/internal/catch_textflow.cpp
@@ -6,6 +6,8 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_textflow.hpp>
+
+#include <algorithm>
#include <cstring>
#include <ostream>
@@ -41,13 +43,16 @@ namespace Catch {
void Column::const_iterator::calcLength() {
m_addHyphen = false;
- const auto maxLineLength = m_column.m_width - indentSize();
m_parsedTo = m_lineStart;
+
std::string const& current_line = m_column.m_string;
if ( current_line[m_lineStart] == '\n' ) {
++m_parsedTo;
}
- while ( m_parsedTo < current_line.size() &&
+
+ const auto maxLineLength = m_column.m_width - indentSize();
+ const auto maxParseTo = std::min(current_line.size(), m_lineStart + maxLineLength);
+ while ( m_parsedTo < maxParseTo &&
current_line[m_parsedTo] != '\n' ) {
++m_parsedTo;
}