fix(mac): correct memory width mismatch in keyCodeForChar #15393
+14
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix a memory corruption bug in
keyCodeForChar()function inmac/TestInput/TestInput/TestInputController.mwhere a 64-bit pointer-sized value was being written into a 16-bitCGKeyCodevariable, causing stack corruption.Problem
The original code had a dangerous pointer/memory width mismatch:
CFDictionaryGetValueIfPresentwrites a pointer-sized value (64 bits on modern systems) to the memory location provided. Casting&code(a pointer to a 16-bit variable) toconst 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:
Solution
Use an intermediate pointer-sized variable to safely receive the dictionary value, then cast to
CGKeyCode:References
Apple Documentation
valueparameter is documented as: "A pointer to memory which should be filled with the pointer-sized value if a matching key is found."64-bit Porting Best Practices
may related issues: #3084 #894 #1005 #1072 #1143 #11674 #11673 #11057