jordansissel / xdotool

fake keyboard/mouse input, window management, and more
Other
3.26k stars 319 forks source link

'key' command causes segfault on MacOS w/ XQuartz #388

Open tpoindex opened 2 years ago

tpoindex commented 2 years ago

'key' (and 'keydown', 'keyup') causes a segfault on MacOS / XQuartz. This is being run inside of an xterm under XQuartz.

$ xdotool  key  --clearmodifiers  Delete
Segmentation fault: 11

Other key presses cause the same issue, 'A', 'F1', 'ctrl+c' etc.

MacOS 12.3.1 (Monterey) XQuartz 2.8.1 (xorg-server 1.20.11) xdotool version 3.20211022.1 (installed via brew)

However, the 'type' command seems to work just fine

$ xdotool type ABC 
ABC
FascinatedBox commented 2 years ago

Can you provide a backtrace with gdb (or some other tool, idk what MacOS has) to help narrow down what's going on when it crashes?

tpoindex commented 2 years ago

Sorry I didn't provide a stacktrace before, I'm not really a Mac developer, but was able to generated one with lldb.

---Version info: $ xdotool -v xdotool version 3.20211022.1

$ brew info xdotool xdotool: stable 3.20211022.1 (bottled), HEAD Fake keyboard/mouse input and window management for X https://www.semicomplete.com/projects/xdotool/ /usr/local/Cellar/xdotool/3.20211022.1 (10 files, 267.4KB) * Poured from bottle on 2022-04-20 at 09:56:03 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/xdotool.rb License: BSD-3-Clause

------Stack trace:

$ ulimit -c unlimited $ xdotool key Backspace (core dump file in /cores/* .....)

$ lldb -c core.15864 /usr/local/bin/xdotool (lldb) target create "/usr/local/bin/xdotool" --core "core.15864" Core file '/Users/thopoi/core.15864' (x86_64) was loaded. (lldb) thread backtrace all

Here's another invocation, this time running in lldb, the error message EXC_BAD_ACCESS makes me wonder if there is a problem with the brew build, or perhaps it's a bogus message ??

$ lldb xdotool key Backspace (lldb) target create "xdotool" Current executable set to 'xdotool' (x86_64). (lldb) settings set -- target.run-args "key" "Backspace" (lldb) r Process 5121 launched: '/usr/local/bin/xdotool' (x86_64) Process 5121 stopped

Btw, the brew formula is: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/xdotool.rb

Seems like this points to: https://github.com/jordansissel/xdotool/blob/735e301665e7f9b8fe850588e88a3a0973695eec/xdo.c#L1391

I found an man page for MacOS strtok_r: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/strtok_r.3.html

I see in the man page example that the tokctx pointer is used to re-assign the starting string buffer pointer.

Hope this helps.

FascinatedBox commented 2 years ago

Hey, thanks for getting back with the backtrace. I'm really puzzled as to why strtok_r is failing here. I ran your examples through my xdotool latest git + valgrind on my Linux machine and I'm not seeing any invalid reads.

I grabbed the tarball from the Homebrew listing (also thanks for that btw) and ran that through valgrind, no issues.

The strtok_r's usage seems correct: It's using a cloned string, first member is set to NULL after the first pass.

Have you tried other commands to see if they give similarly strange crashes, or it is just key?

tpoindex commented 2 years ago

Just 'key', which is the only usage of strtok_r in xdotool.

Another data point, 'xdotool key xxxx' works fine on modern FreeBSD (12 & 13). I'm still guessing some weirdness with Apple's library implementation. The Apple strtok_r man page I listed above says "Based on the FreeBSD 3.0 implementation."

Good news is that I don't really rely on xdotool on MacOS. I discovered the problem while testing another open source program, https://github.com/ideasman42/nerd-dictation, which uses xdotool (both 'key' and 'type'). I modified that program to just output to stdout immediately, which normally 'defers' all stdout output until exit.

Feel free to close this issue if you want.