octalmage / robotjs

Node.js Desktop Automation.
http://robotjs.io
MIT License
12.31k stars 951 forks source link

keyToggle: EXC_BAD_ACCESS at createStringForKey + 68 #570

Closed anatawa12 closed 4 years ago

anatawa12 commented 4 years ago

Current Behavior

segment fault when call keyToggle

top of exception log (when run with electron) ``` Process: Electron [76393] Path: /Users/USER/*/Electron.app/Contents/MacOS/Electron Identifier: com.github.Electron Version: 6.1.9 (6.1.9) Code Type: X86-64 (Native) Parent Process: ??? [76390] Responsible: idea [70182] User ID: 502 Date/Time: 2020-04-08 17:36:35.112 +0900 OS Version: Mac OS X 10.15.3 (19D76) Report Version: 12 Anonymous UUID: 54C4EA52-536E-29AE-6553-AA8E627D0084 Sleep/Wake UUID: ED356708-3064-469F-BDD2-EA75878EEB67 Time Awake Since Boot: 5000000 seconds Time Since Wake: 63000 seconds System Integrity Protection: disabled Crashed Thread: 0 CrBrowserMain Dispatch queue: com.apple.main-thread Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Termination Signal: Segmentation fault: 11 Termination Reason: Namespace SIGNAL, Code 0xb Terminating Process: exc handler [76393] VM Regions Near 0: --> __TEXT 0000000104213000-000000010423d000 [ 168K] r-x/rwx SM=COW /Users/USER/*/Electron.app/Contents/MacOS/Electron Thread 0 Crashed:: CrBrowserMain Dispatch queue: com.apple.main-thread 0 com.apple.CoreFoundation 0x00007fff3b1e1c9f CFDataGetBytePtr + 6 1 robotjs.node 0x000000010f806b53 createStringForKey + 68 2 robotjs.node 0x000000010f806a90 keyCodeForChar + 82 3 robotjs.node 0x000000010f802948 CheckKeyCodes(char*, unsigned short*) + 44 4 robotjs.node 0x000000010f8031b1 keyToggle(Nan::FunctionCallbackInfo const&) + 525 5 robotjs.node 0x000000010f805d05 Nan::imp::FunctionCallbackWrapper(v8::FunctionCallbackInfo const&) + 186 6 com.github.Electron.framework 0x0000000104e382ac 0x104248000 + 12518060 ```

Possible Solution

Steps to Reproduce (for bugs)

run this source code with node or with electron.

const robot = require("robotjs");

robot.keyToggle("a", "down");

Your Environment

with node.js

with electron

bk138 commented 4 years ago

This is caused by https://github.com/octalmage/robotjs/blob/b26c7ee0e3dd05420370350e540c48ae9d38f06c/src/keycode.c#L98, where sizeof(void*) bytes (probably 8) are written to a memory segment of 2 bytes (a 16-bit CGKeycode variable), resulting in all kinds of weird behaviour. A fix is to make code a bigger variable, size_t for instance. The flaky code snippet source is https://stackoverflow.com/a/1971027/361413