fix: keyTap throws "Invalid key code" for valid keys due to dangling pointer#792
Closed
ssxv wants to merge 1 commit into
Closed
fix: keyTap throws "Invalid key code" for valid keys due to dangling pointer#792ssxv wants to merge 1 commit into
ssxv wants to merge 1 commit into
Conversation
Author
|
Please check this. Key Tap isn't working. Its one of basic functionalities of robotjs which is breaking In v0.7.0 |
Owner
|
Thanks for the PR! I had fixed this in #784 and just cherry-picked the commit into master (as well as a few other build fixes). I will work on getting a fix deployed to npm! |
Owner
|
robotjs@0.7.1 deployed to npm which resolve this issue, thanks again!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue: #789
Description
robot.keyTap("tab")and other valid multi-character key names throwError: Invalid key code specifieddue to a dangling pointer bug inkeyTapWrapper.Root Cause
In
src/robotjs.cc, thekeyTapWrapperfunction was doing this:kstr.Utf8Value()returns a temporarystd::string. Taking.c_str()from it gives a raw pointer that becomes invalid as soon as the temporary is destroyed - at the end of that same statement. By the timekis passed toCheckKeyCodes(), it is a dangling pointer pointing to freed memory, causing the key name lookup against thekey_namestable to fail unpredictably.This is a non-deterministic bug - it may work in some environments depending on whether the freed memory has been overwritten.
Fix
Changed
keyTapWrapperto store the string in a namedstd::stringthat lives for the full function scope, matching the pattern already used inkeyToggleWrapper:std::string kstr = info[0].As<Napi::String>(); k = kstr.c_str();Files Changed
Testing
Verified that
robot.keyTap("tab")and other named keys (enter, backspace, escape, etc.) no longer throw Invalid key code specified after the fix.