Fix garbage surrounding-text offsets reaching input methods
maliit-keyboard keeps crashing under kwin (bug 516195 and its duplicates) because it receives surrounding-text cursor/anchor offsets that do not fit the text, and uses them as lengths and pointer offsets. There are two distinct sources for such offsets in kwin, and this fixes both.
What actually reaches the input method. Recovered from the wayland receive buffers of two maliit-keyboard core dumps (two different machines, Plasma 6.6.6, touch-only POS terminals), the surrounding_text event that kills maliit is:
machine A: text="" cursor=0 anchor=1197997216 (0x4767FCA0)
machine B: text="" cursor=0 anchor=2688617760 (0xA0410D20)Empty text with a huge, per-machine-different anchor. This also rules out the "overly large surrounding text" theory from the bug title: the wire format caps messages at 4096 bytes, and the captured messages are 24 bytes with zero bytes of text - the gigabyte allocation that aborts maliit matches the anchor value, in the code paths that use it as a length.
Commit 1: initialize the internal context. The buffer chain (surrounding_text → content_type → commit_state) matches InputMethod::adoptInputMethodContext() exactly, and its internal-context branch reads InternalInputMethodContext::m_cursorPos/m_anchorPos - assigned only in update(), never initialized, while isEnabled() merely requires a focused internal window accepting input. Two machines, two different garbage values: the signature of uninitialized reads.
Commit 2: clamp at the chokepoint. Offsets supplied by text-input clients are forwarded verbatim as well, so a misbehaving client produces the same crash through the text-input path - bug 517620 documents Discord/Electron doing exactly that. Clamping in InputMethodContextV1Interface::sendSurroundingText() closes the class regardless of origin: depending on the garbage value maliit dies via giant allocation (qBadAlloc, Qt only special-cases a length of exactly -1) or via pointer arithmetic past the array (SEGV).
maliit should of course also survive bogus offsets; a corresponding clamp has been running on the two machines above since Aug 27 and will be submitted to maliit/framework separately.