Repository navigation
Fix use of freed binding in key rebinding - #1739
Open
NoahBPeterson wants to merge 1 commit into
Open
NoahBPeterson wants to merge 1 commit into
NoahBPeterson wants to merge 1 commit into
Conversation
handler_rebind_keys_add() saves prevcmd = cmdbind_get(key) and then calls bind_key(). Binding "nothing" makes bind_key() call cmdbind_remove(), which frees that binding, and the message code then reads prevcmd->cmd and prevcmd->cmd->ef_txt from freed memory. Rebinding an already-bound key to another command updates the binding in place, so prevcmd->cmd already equals the new command afterwards and "Changed key" is never printed. Note the previous command before calling bind_key() and use that. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Follow-up to my report #1650 (fixed by c3ce341 and 411c8be). With
cmdstrinitialized, the same function has two more problems.1. Unbinding a key reads freed memory. Choosing "nothing" for a bound key:
prevcmd = cmdbind_get(key)points to the key'sstruct Cmd_bindbind_key(key, "nothing", TRUE)callscmdbind_remove(key), whichfree()s that structif (prevcmd && prevcmd->cmd != ec)andprevcmd->cmd->ef_txtthen read the freed structWhat is printed depends on the allocator. With the 5.0.0 release plus the #1650 fix, on macOS arm64, the freed
cmdfield read back as NULL, so no message appeared. A recording of the same keystrokes made on another machine printedChanged key 'K' from "runnorth" to "nothing".2. Rebinding a bound key never reports the change. Rebinding
K(runnorth) toapplymakescmdbind_add()update the same struct in place, so afterwardsprevcmd->cmd == ecand nothing is printed.The fix notes the previous command before calling
bind_key()and uses that. Both cases now print the message the code intends:Changed key 'K' from "runnorth" to "nothing".Changed key 'K' from "runnorth" to "apply".A binding is removed rather than kept with a NULL
cmd, so testingprevecinstead ofprevcmdchanges nothing else. The PR also adds an entry todoc/fixes5-0-1.txt.To reproduce:
#optionsfull, select "bind keys",a(bind key to a command),K, thena(nothing) to unbind, or:applyEnter to rebind.Tested with the same change applied to the 5.0.0 release source (16ff591, where this function is identical) on macOS arm64 with clang. The unbind and rebind sessions print the messages above, and 106 other recorded sessions are unchanged. I have not built the current
NetHack-5.0branch.🤖 Generated with Claude Code