Skip to content

Conversation

@PekingSpades
Copy link
Contributor

@PekingSpades PekingSpades commented Dec 24, 2025

Description

Fix SIGSEGV/SIGBUS crashes in macOS keyboard functions.

Fixes: #690, #732, #486

Related Issues Analysis

Issue Title Environment Symptom
#690 Keyboard Function Crashes on macOS macOS 15.0, M3 (arm64), Go 1.22.4 SIGSEGV/SIGBUS in _Cfunc_keyCodeForChar() when calling KeyTap/KeyToggle/KeyUp/KeyDown
#732 robotgo.KeyTap Fails on Macos X86 macOS X86, Go 1.24.0 SIGSEGV at address 0x8 in _Cfunc_keyCodeForChar(0x76) when calling KeyTap("v","cmd")
#486 KeyTap Segmentation Violation macOS 10.13.4 ~ 14.4+, Intel & Apple Silicon Long-standing issue (since 2022) with 13+ reports, crash in keyCodeForChar()

Common pattern: All three issues crash at the same location (keyCodeForChar) with SIGSEGV at address 0x8, which is the signature of stack corruption from writing 8 bytes into a 2-byte variable.

Root Cause

In keyCodeForChar(), the code passes a CGKeyCode* (2 bytes) to CFDictionaryGetValueIfPresent() which expects const void** (8 bytes on 64-bit). This overwrites adjacent stack memory, causing crashes when critical data is affected.

Technical details with official documentation:

When CFDictionaryGetValueIfPresent() writes 8 bytes into a 2-byte CGKeyCode variable, it corrupts 6 bytes of adjacent stack memory.

The crash manifests on different macOS configurations depending on stack layout (compiler version, optimization level, ABI). Affected reports span:

Changes

key/keycode_c.h:

  1. keyCodeForChar(): Use pointer-sized variable for CFDictionaryGetValueIfPresent()
  2. createStringForKey(): Add NULL check for TISCopyCurrentKeyboardLayoutInputSource() and fix memory leak when layoutData is nil

References

Testing


Signed-off-by: PekingSpades [email protected]

Summary by CodeRabbit

  • Bug Fixes
    • Improved keyboard handling stability on macOS with enhanced pointer management
    • Fixed potential memory leak in keyboard layout data processing
    • Added safety checks to prevent crashes from null keyboard data

✏️ Tip: You can customize this high-level summary in your review settings.

@CLAassistant
Copy link

CLAassistant commented Dec 24, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link

coderabbitai bot commented Dec 24, 2025

📝 Walkthrough

Walkthrough

The fix addresses 64-bit pointer handling and memory management issues in keyboard code mapping on macOS. It improves safety in keyCodeForChar by using CFDictionaryGetValueIfPresent with a pointer-sized temporary, and adds NULL checks with proper resource cleanup in createStringForKey to prevent crashes and memory leaks.

Changes

Cohort / File(s) Summary
Keyboard pointer handling safety
key/keycode_c.h
Replaced direct pointer casting with pointer-sized temporary (codePtr) in keyCodeForChar for safer 64-bit value retrieval; added NULL checks for currentKeyboard in createStringForKey with early return; added layoutData NULL check with currentKeyboard cleanup to prevent memory leak

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Pointers behaved so very wild,
Causing crashes, unreconciled,
Now with checks both safe and sound,
Keyboard keys won't crash around!
Memory leaks now cleaned with care, 🔧✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description is comprehensive, well-structured, and provides detailed context including root cause analysis, technical details, changes made, and testing information.
Linked Issues check ✅ Passed The PR addresses SIGSEGV/SIGBUS crashes in macOS keyboard functions by fixing pointer size mismatch in CFDictionaryGetValueIfPresent() calls and adding NULL checks for keyboard layout data, directly resolving the crashes reported in #690.
Out of Scope Changes check ✅ Passed All changes in key/keycode_c.h are directly related to fixing the stack corruption issue and memory leak in keyboard functions, with no extraneous modifications.
Title check ✅ Passed The title accurately describes the main change: fixing SIGSEGV/SIGBUS crashes in macOS keyboard functions, which is the primary objective of the pull request.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

@PekingSpades PekingSpades changed the title Fix: fix stack overflow and memory leak in macOS keycode functions Fix: fix SIGSEGV/SIGBUS crash in macOS keyboard functions Dec 24, 2025
@vcaesar vcaesar added the bug label Dec 24, 2025
@vcaesar vcaesar added this to the v1.10.0 milestone Dec 24, 2025
@vcaesar vcaesar merged commit 5a38884 into go-vgo:master Dec 28, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants