Wayland: a key whose text needs 16 bytes or more reaches onTextInput cut inside a character and padded with NULs #205

Open
opened 2026-11-27 08:01:33 +00:00 by jorijnvdgraaf · 0 comments

Summary

Device::keyboard_key asks libxkbcommon for a key's text in a 16-byte buffer, and then keeps as many bytes as the call says the text needs, not as many as it wrote. For a text of 16 bytes or more, onTextInput and the key's repeats get the first 15 bytes, cut wherever they end (inside a UTF-8 sequence too), followed by NULs up to the length the call returned. Line numbers refer to 148ed01.

Details

  • implementations/Crafter.Graphics-Device.cpp:769-776: buf.resize(16), n = xkb_state_key_get_utf8(xkb_state, xkbKeycode, buf.data(), 16), and for n > 0 with a first byte that is no control character buf.resize(n), utf8 = buf and onTextInput.Invoke(utf8). The repeat keeps the same string (:783), and TickKeyRepeats sends it again with every repeat (:846-847).
  • xkb_state_key_get_utf8 returns "the number of bytes required for the string, excluding the NULL byte", truncates into a buffer that is too small "(though still NULL-terminated)", and says to check for truncation by comparing the return value with the buffer's size, "similarly to the snprintf(3) function" (/usr/include/xkbcommon/xkbcommon.h, libxkbcommon 1.13.2).
  • One keysym's UTF-8 is at most 4 bytes, so this takes a level with several keysyms, which libxkbcommon has as an extension to XKB: "this function can return more than one keysym" (xkb_state_key_get_syms in the same header).

Concrete case

A keymap whose <AC01> has one level of five keysyms, [ { U1F600, U1F601, U1F602, U1F603, U1F604 } ], compiled and queried with libxkbcommon 1.13.2 in a standalone program: xkb_state_key_get_syms gives 5 keysyms, xkb_state_key_get_utf8(state, 38, NULL, 0) gives 20, and into a 16-byte buffer it writes f0 9f 98 80 f0 9f 98 81 f0 9f 98 82 f0 9f 98 00 and returns 20. With that, keyboard_key passes on 20 bytes: three emoji, the first three bytes of the fourth, and five NULs. Not run through the library.

Suggested fix

Ask for the length first (xkb_state_key_get_utf8(state, key, nullptr, 0)), make the string that long, and fill it with a second call given that length plus one. Or keep the 16 bytes and treat a return value of 16 or more as no text.

## Summary `Device::keyboard_key` asks libxkbcommon for a key's text in a 16-byte buffer, and then keeps as many bytes as the call says the text needs, not as many as it wrote. For a text of 16 bytes or more, `onTextInput` and the key's repeats get the first 15 bytes, cut wherever they end (inside a UTF-8 sequence too), followed by NULs up to the length the call returned. Line numbers refer to `148ed01`. ## Details - `implementations/Crafter.Graphics-Device.cpp:769-776`: `buf.resize(16)`, `n = xkb_state_key_get_utf8(xkb_state, xkbKeycode, buf.data(), 16)`, and for `n > 0` with a first byte that is no control character `buf.resize(n)`, `utf8 = buf` and `onTextInput.Invoke(utf8)`. The repeat keeps the same string (`:783`), and `TickKeyRepeats` sends it again with every repeat (`:846-847`). - `xkb_state_key_get_utf8` returns "the number of bytes required for the string, excluding the `NULL` byte", truncates into a buffer that is too small "(though still `NULL`-terminated)", and says to check for truncation by comparing the return value with the buffer's size, "similarly to the `snprintf(3)` function" (`/usr/include/xkbcommon/xkbcommon.h`, libxkbcommon 1.13.2). - One keysym's UTF-8 is at most 4 bytes, so this takes a level with several keysyms, which libxkbcommon has as an extension to XKB: "this function can return more than one keysym" (`xkb_state_key_get_syms` in the same header). ## Concrete case A keymap whose `<AC01>` has one level of five keysyms, `[ { U1F600, U1F601, U1F602, U1F603, U1F604 } ]`, compiled and queried with libxkbcommon 1.13.2 in a standalone program: `xkb_state_key_get_syms` gives 5 keysyms, `xkb_state_key_get_utf8(state, 38, NULL, 0)` gives 20, and into a 16-byte buffer it writes `f0 9f 98 80 f0 9f 98 81 f0 9f 98 82 f0 9f 98 00` and returns 20. With that, `keyboard_key` passes on 20 bytes: three emoji, the first three bytes of the fourth, and five NULs. Not run through the library. ## Suggested fix Ask for the length first (`xkb_state_key_get_utf8(state, key, nullptr, 0)`), make the string that long, and fill it with a second call given that length plus one. Or keep the 16 bytes and treat a return value of 16 or more as no text.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#205
No description provided.