Repository navigation
fix(encodes): fix heap buffer overflow read in detectCharset #756
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
|
||
| 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') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Malformed input reads past buffer When Bound reads in every locale-specific fallback scanner by the Also at Prompt for AI agentsIn `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`.There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Raw byte arrays still overread input When the caller passes a Make Prompt for AI agentsIn `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) | ||
| { | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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'"; | ||
|
|
||
There was a problem hiding this comment.
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_MAXbytes and the scan does not stop earlier,posoverflows itsintrange while the loop compares it with the largersize, so subsequent indexing reads outside the buffer and charset detection can crash or behave incorrectly.Declare
posasqsizetypeso it can represent the full range ofsize.Prompt for AI agents