Inspect console: fix infinite loop in pagination

When given a string that includes newlines but doesn't end with a newline,
the old code would get stuck in an infinite loop.

Fixes #7194.
This commit is contained in:
Steve Cotton 2022-12-05 08:59:13 +01:00 committed by Steve Cotton
parent ddbf533aba
commit b5ff5c3ddc

View file

@ -110,15 +110,22 @@ private:
{
pages.clear();
std::size_t start = 0;
while(start < data.size()) {
while(start + max_inspect_win_len < data.size()) {
// This could search into data that's already on a previous page, which is why the result
// is then checked for end < start.
std::size_t end = data.find_last_of('\n', start + max_inspect_win_len);
if(end == std::string::npos) {
end = data.size() - 1;
int len;
if(end == std::string::npos || end < start) {
len = max_inspect_win_len;
} else {
len = end - start + 1;
}
int len = end - start + 1;
pages.emplace_back(start, len);
start += len;
}
if(start < data.size()) {
pages.emplace_back(start, data.size() - start);
}
}
static const unsigned int max_inspect_win_len = 20000;
std::string data;