readline: fix crash when navigating wrapped multiline history - #65610
Open
piyushrajyadav wants to merge 1 commit into
Open
readline: fix crash when navigating wrapped multiline history#65610piyushrajyadav wants to merge 1 commit into
piyushrajyadav wants to merge 1 commit into
Conversation
When navigating multiline history in readline/REPL with the UP/DOWN arrow keys, [kMultilineMove] used getCursorPos() (which returns the terminal screen's visual row and column index) to index into the logical \n-delimited splitLines array. When a logical line was long enough to wrap across multiple terminal columns, cursorPos.rows exceeded splitLines.length - 1, causing splitLines[cursorPos.rows] to be undefined and throwing: TypeError: Cannot read properties of undefined (reading 'length') This commit adds getLogicalCursorPos() to compute the logical row and column based on \n characters in this.line and uses it across multiline history navigation. Fixes: nodejs#59431 Signed-off-by: piyushrajyadav <piyushyadavrajyadav@gmail.com> Assisted-by: Antigravity <antigravity@google.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.
Fixes: #59431
Description
When navigating multiline history in
readline/ REPL using the<UP>/<DOWN>arrow keys,[kMultilineMove]previously usedthis.getCursorPos()to determine the row index of the cursor.However,
getCursorPos()returns the visual/display row coordinates on the terminal screen (which counts visual line wraps when a logical line exceedsthis.columns). In contrast,splitLinesis the array of logical lines delimited by\n.When an entry contained a logical line long enough to wrap across multiple terminal columns,
cursorPos.rowsexceededsplitLines.length - 1. As a result,splitLines[cursorPos.rows]resolved toundefined, throwing:and terminating the REPL process.
This change:
getLogicalCursorPos()tolib/internal/readline/interface.jsto compute the logical line index and column offset based on\ncharacter positions inthis.line.[kMultilineMove],[kMoveUpOrHistoryPrev], and[kMoveDownOrHistoryNext]to use logical coordinates.test/parallel/test-readline-interface.js.Verification
test/parallel/test-readline-interface.js.StringPrototypeLastIndexOf).