Skip to content
Merged
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
22 changes: 16 additions & 6 deletions src/common/encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,15 @@
{
qCDebug(appLog) << "Entering detectCharset";
const char* text = byteArray.constData();
uint8_t c = *text;
const qsizetype size = byteArray.size();
int pos = 0;

Check warning on line 748 in src/common/encoding.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The scope of the variable 'pos' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:\012void f(int x)\012{\012 int i = 0;\012 if (x) {\012 // it's safe to move 'int i = 0;' here\012 for (int n = 0; n < 10; ++n) {\012 // it is possible but not safe to move 'int i = 0;' here\012 do_something(&i);\012 }\012 }\012}\012When you see this message it is always safe to reduce the variable scope 1 level.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Large inputs corrupt scan position

When a valid input exceeds INT_MAX bytes and the scan does not stop earlier, pos overflows its int range while the loop compares it with the larger size, so subsequent indexing reads outside the buffer and charset detection can crash or behave incorrectly.

Declare pos as qsizetype so it can represent the full range of size.

Prompt for AI agents
In `src/common/encoding.cpp` at line 748:

**Large inputs corrupt scan position**

When a valid input exceeds `INT_MAX` bytes and the scan does not stop earlier, `pos` overflows its `int` range while the loop compares it with the larger `size`, so subsequent indexing reads outside the buffer and charset detection can crash or behave incorrectly.

Declare `pos` as `qsizetype` so it can represent the full range of `size`.

uint8_t c = 0;
std::string charset;

if (validateUTF8 (byteArray))
{
qCDebug(appLog) << "validateUTF8 is true";
while ((c = *text++) != '\0')
while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Malformed input reads past buffer

When validateUTF8 returns false and a locale-specific fallback scanner receives a truncated multibyte sequence, the locale-specific fallback scanners consume the implicit terminator and then read past the QByteArray allocation, so charset detection can crash.

Bound reads in every locale-specific fallback scanner by the QByteArray size.

Also at src/common/encoding.cpp:747-748.

Prompt for AI agents
In `src/common/encoding.cpp` at line 755:

**Malformed input reads past buffer**

When `validateUTF8` returns false and a locale-specific fallback scanner receives a truncated multibyte sequence, the locale-specific fallback scanners consume the implicit terminator and then read past the `QByteArray` allocation, so charset detection can crash.

Bound reads in every locale-specific fallback scanner by the `QByteArray` size.

Also at `src/common/encoding.cpp:747-748`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Raw byte arrays still overread input

When the caller passes a QByteArray::fromRawData whose backing bytes have no NUL terminator, detectCharset calls validateUTF8 before this bounded scan, and validateUTF8 reads constData() until it finds a NUL without checking the array size. A non-NUL-terminated QByteArray::fromRawData therefore causes an out-of-bounds read before this loop runs.

Make validateUTF8 scan by length and check the remaining length before reading UTF-8 continuation bytes.

Prompt for AI agents
In `src/common/encoding.cpp` at line 755:

**Raw byte arrays still overread input**

When the caller passes a `QByteArray::fromRawData` whose backing bytes have no NUL terminator, `detectCharset` calls `validateUTF8` before this bounded scan, and `validateUTF8` reads `constData()` until it finds a NUL without checking the array size. A non-NUL-terminated `QByteArray::fromRawData` therefore causes an out-of-bounds read before this loop runs.

Make `validateUTF8` scan by length and check the remaining length before reading UTF-8 continuation bytes.

{
if (c > 0x7F)
{
Expand All @@ -761,11 +763,15 @@
if (c == 0x1B) /* ESC */
{
qCDebug(appLog) << "c == 0x1B";
c = *text++;
if (pos >= size)
break;
c = static_cast<uint8_t>(text[pos++]);
if (c == '$')
{
qCDebug(appLog) << "c == '$'";
c = *text++;
if (pos >= size)
break;
c = static_cast<uint8_t>(text[pos++]);
switch (c)
{
case 'B': // JIS X 0208-1983
Expand All @@ -778,7 +784,9 @@
charset = "ISO-2022-JP-2";
break;
case '(':
c = *text++;
if (pos >= size)
break;
c = static_cast<uint8_t>(text[pos++]);
switch (c)
{
case 'C': // KSC5601-1987
Expand All @@ -788,7 +796,9 @@
}
break;
case ')':
c = *text++;
if (pos >= size)
break;
c = static_cast<uint8_t>(text[pos++]);
if (c == 'C')
{
qCDebug(appLog) << "c == 'C'";
Expand Down
Loading