Skip to content

Conversation

@PekingSpades
Copy link

Summary

Fix a memory corruption bug in keyCodeForChar() function in mac/TestInput/TestInput/TestInputController.m where a 64-bit pointer-sized value was being written into a 16-bit CGKeyCode variable, causing stack corruption.

Problem

The original code had a dangerous pointer/memory width mismatch:

CGKeyCode code;  // uint16_t, 16 bits
// ...
CFDictionaryGetValueIfPresent(charToCodeDict, charStr, (const void **)&code)

CFDictionaryGetValueIfPresent writes a pointer-sized value (64 bits on modern systems) to the memory location provided. Casting &code (a pointer to a 16-bit variable) to const void ** causes the function to write 8 bytes into a 2-byte memory location, corrupting 6 bytes of adjacent stack memory.

This is undefined behavior and can lead to:

  • Stack corruption
  • Random crashes
  • Incorrect return values

Solution

Use an intermediate pointer-sized variable to safely receive the dictionary value, then cast to CGKeyCode:

const void *value = NULL;
CGKeyCode code;
if (CFDictionaryGetValueIfPresent(charToCodeDict, charStr, &value)) {
    code = (CGKeyCode)(uintptr_t)value;
} else {
    code = UINT16_MAX;
}

References

Apple Documentation

64-bit Porting Best Practices

may related issues: #3084 #894 #1005 #1072 #1143 #11674 #11673 #11057

@github-project-automation github-project-automation bot moved this to Todo in Keyman Dec 31, 2025
@keymanapp-test-bot keymanapp-test-bot bot added the user-test-missing User tests have not yet been defined for the PR label Dec 31, 2025
@keymanapp-test-bot
Copy link

User Test Results

Test specification and instructions

ERROR: user tests have not yet been defined

@keymanapp-test-bot keymanapp-test-bot bot added this to the A19S19 milestone Dec 31, 2025
@keyman-server
Copy link
Collaborator

This pull request is from an external repo and will not automatically be built. The build must still be passed before it can be merged. Ask one of the team members to make a manual build of this PR.

@mcdurdin mcdurdin requested a review from sgschantz December 31, 2025 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

user-test-missing User tests have not yet been defined for the PR

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants