Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed size comparisons that could lead to infinite loops #18451

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/buffer/out/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ OutputCellIterator ROW::WriteCells(OutputCellIterator it, const til::CoordType c
auto colorStarts = gsl::narrow_cast<uint16_t>(columnBegin);
auto currentIndex = colorStarts;

while (it && currentIndex <= finalColumnInRow)
// finalColumnInRow is 32-bit and currentIndex is 16-bit. If finalColumnInRow is higher
// than the 16-bit integer limit, we will loop indefinitely.
while (it && currentIndex <= saturated_cast<uint16_t>(finalColumnInRow))
Comment on lines +436 to +438
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tested this, but looking at just the code, it seems to me that simply removing the narrow_cast above would have the same effect of fixing this bug.

{
// Fill the color if the behavior isn't set to keeping the current color.
if (it->TextAttrBehavior() != TextAttributeBehavior::Current)
Expand Down
4 changes: 3 additions & 1 deletion src/terminal/adapter/SixelParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,9 @@ void SixelParser::_updateTextColors()
// the text output as well.
if (_conformanceLevel <= 3 && _maxColors > 2 && _colorTableChanged) [[unlikely]]
{
for (IndexType tableIndex = 0; tableIndex < _maxColors; tableIndex++)
// _maxColors is 64-bit and tableIndex is 8-bit. If _maxColors is higher
// than the 8-bit integer limit, we will loop indefinitely.
for (IndexType tableIndex = 0; tableIndex < (saturated_cast<uint8_t>(_maxColors)); tableIndex++)
Comment on lines 611 to +615
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this situation _maxColors will never be greater than 16, because that's the maximum value for conformance level 3 (which we're checking in the condition above). Would it satisfy the CodeQL scan if we also added a _maxColors <= 16 check to that condition? That seems like a preferable fix to me, because it makes it clear what values we're actually dealing with here. Suggesting that _maxColors has a 64-bit range here is very misleading.

{
_dispatcher.SetColorTableEntry(tableIndex, _colorFromIndex(tableIndex));
}
Expand Down
2 changes: 1 addition & 1 deletion src/terminal/adapter/charsets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft::Console::VirtualTerminal
public:
constexpr CharSet(const std::initializer_list<std::pair<wchar_t, wchar_t>> replacements)
{
for (auto i = L'\0'; i < _translationTable.size(); i++)
for (size_t i = L'\0'; i < _translationTable.size(); i++)
_translationTable.at(i) = BaseChar + i;
for (auto replacement : replacements)
_translationTable.at(replacement.first - BaseChar) = replacement.second;
Expand Down
Loading