qtbase: fix crash in QString::at on malformed unicode#249
Open
overdeadsss wants to merge 1 commit into
Open
Conversation
Contributor
|
Looks like you're fixing the problem at the wrong level? It's an API contract that one must not pass out-of-bounds index. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
The current implementation of
QString::at(int i)andQString::operator[](int i)in qtbase relies on Q_ASSERT for bounds checking. In release builds, these asserts are often compiled out or lead to undefined behavior/crashes when the text layout engine (e.g., during HarfBuzz shaping or complex text rendering) attempts to access an index outside the string's actual bounds.This is particularly reproducible when the application processes specific complex Unicode sequences, such as extremely long Arabic ligatures or malformed RTL/LTR sequences, which can confuse the layout engine's index calculations.
Example of "Unicode bombs" causing the crash:
The crash can be triggered by sending or viewing a music-message containing many repeated complex ligatures in title or description, for example:
U+FDFD (Arabic Ligature Bismillah Ar-Rahman Ar-Rahim): ﷽
Combinations of ZWJ (Zero Width Joiner) and multiple skin tone modifiers.
Long sequences of combining marks.
A string like ﷽﷽﷽... (repeated multiple times) often leads to out-of-bounds access during the shaping phase.
Solution:
This patch introduces explicit bounds checking using
uint(i)comparison. By casting the index tounsigned int, we simultaneously check if the index is negative or greater than/equal to the string size.If the index is out of bounds, the function now returns a default-constructed
QChar()instead of triggering a fatal crash.Testing:
Built tdesktop with this patched version of qtbase.
Sent the "Bismillah" (U+FDFD) sequence mentioned above.
Result: The application remains stable, the text may not render perfectly, but the process does not terminate.