Fix ChatPrompt crash in very narrow windows (#13305)

In very narrow windows, `m_cols` can be small (i.e. 0).
Hence, `m_view <= m_line.size() + 1 - m_cols` does not guarantee
`m_view <= m_line.size()`.
`std::string::substr(pos, npos)` requires `pos <= size()`.
This commit is contained in:
DS 2023-03-11 17:46:49 +01:00 committed by GitHub
parent 1aeb0280df
commit b1ed0ef721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -229,8 +229,8 @@ void ChatBuffer::scrollBottom()
m_scroll = getBottomScrollPos(); m_scroll = getBottomScrollPos();
} }
u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols, u32 ChatBuffer::formatChatLine(const ChatLine &line, u32 cols,
std::vector<ChatFormattedLine>& destination) const std::vector<ChatFormattedLine> &destination) const
{ {
u32 num_added = 0; u32 num_added = 0;
std::vector<ChatFormattedFragment> next_frags; std::vector<ChatFormattedFragment> next_frags;
@ -269,7 +269,10 @@ u32 ChatBuffer::formatChatLine(const ChatLine& line, u32 cols,
// Very long names // Very long names
hanging_indentation = 2; hanging_indentation = 2;
} }
//EnrichedString line_text(line.text); // If there are no columns remaining after the indentation (window is very
// narrow), we can't write anything
if (hanging_indentation >= cols)
return 0;
next_line.first = true; next_line.first = true;
// Set/use forced newline after the last frag in each line // Set/use forced newline after the last frag in each line
@ -670,7 +673,11 @@ void ChatPrompt::reformat(u32 cols)
std::wstring ChatPrompt::getVisiblePortion() const std::wstring ChatPrompt::getVisiblePortion() const
{ {
return m_prompt + getLineRef().substr(m_view, m_cols); const std::wstring &line_ref = getLineRef();
if ((size_t)m_view >= line_ref.size())
return m_prompt;
else
return m_prompt + line_ref.substr(m_view, m_cols);
} }
s32 ChatPrompt::getVisibleCursorPosition() const s32 ChatPrompt::getVisibleCursorPosition() const