ydotool icon indicating copy to clipboard operation
ydotool copied to clipboard

Bug: Out of bounds read in `ydotool type`

Open AstroSnail opened this issue 10 months ago • 0 comments

When ydotool type is asked to type unicode characters, sometimes strange keypresses are generated.

For example, on my computer, asking ydotool to type "Å" causes it to press the Numpad 0/Insert key:

[erry@soon:~]$ ydotool type Å
^[[2~

This is because inside Client/tool_type.c, the loop that reads characters and transforms them into keycodes doesn't correctly handle non-ASCII characters. Specifically:

The result is that any time a byte greater than 127 is read, the array is indexed out of bounds.

On my system, "Å" is encoded in UTF-8 as the sequence 0xC3 0x85, which ydotool type uses to index the array out of bounds in two separate cases, and one of the out-of-bounds reads produces 0x73250052. As the function takes the lower 16 significant bits of the value as the keycode (and the most significant bit is unset, so it is not considered uppercase), this results in pressing keycode 0x0052 (decimal 82), which corresponds to Numpad 0 (if Num Lock is off, this is the same as Insert).

I'm disappointed that ydotool does not support typing unicode characters, but I'm ready to admit that, at this stage, supporting them will involve a lot of work. Therefore I think the easiest fix would be to add an extra check before calling type_char that ensures the byte is within the range of 0 to 127.

AstroSnail avatar Jan 29 '25 18:01 AstroSnail