Skip to content

Guard surrounding_text against out-of-bounds cursor/anchor offsets#133

Open
thereisnotime wants to merge 1 commit into
maliit:masterfrom
thereisnotime:fix-surrounding-text-oob
Open

Guard surrounding_text against out-of-bounds cursor/anchor offsets#133
thereisnotime wants to merge 1 commit into
maliit:masterfrom
thereisnotime:fix-surrounding-text-oob

Conversation

@thereisnotime

Copy link
Copy Markdown

The Wayland input-method surrounding_text handler in connection/waylandinputmethodconnection.cpp uses the compositor-supplied cursor and anchor byte offsets directly as QString::fromUtf8 lengths, with no bounds check:

m_stateInfo[CursorPositionAttribute] = QString::fromUtf8(utf8_text.constData(), cursor).size();
m_stateInfo[AnchorPositionAttribute] = QString::fromUtf8(utf8_text.constData(), anchor).size();
...
m_selection = QString::fromUtf8(utf8_text.constData() + begin, end - begin);

When a compositor sends an offset larger than the text (KWin on Plasma 6 sends values like 6881396), fromUtf8 reads past the end of the buffer and the process segfaults in _mm_loadu_si128. This is the frequent maliit-keyboard crash reported in maliit/keyboard#262 (and earlier in #86), and it makes the keyboard drop out constantly on Fedora 43 + Plasma 6 Wayland.

This clamps cursor and anchor to the actual UTF-8 length before they are used as offsets/lengths, which also keeps the begin/end selection slice in bounds. It is purely defensive: valid offsets are unchanged.

Refs maliit/keyboard#262.

The compositor's cursor and anchor byte offsets are passed straight into
QString::fromUtf8 as lengths with no validation. A bogus offset larger than the
text (KWin on Plasma 6 sends values like 6881396) makes fromUtf8 read past the
end of the buffer and segfault, which is the frequent maliit-keyboard crash in
maliit/keyboard#262. Clamp cursor and anchor to the text length before use.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant