fix(table): prevent panic when YOffset is set to a negative value - #727
Open
bunlongheng wants to merge 1 commit into
Open
fix(table): prevent panic when YOffset is set to a negative value#727bunlongheng wants to merge 1 commit into
bunlongheng wants to merge 1 commit into
Conversation
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.
What
Table.YOffsetstores whatever integer it is given without validation. When thevalue is negative,
Render()panics with an out-of-range slice index.The crash happens in
resizer.visibleRowIndexes(table/resizing.go). The scrollwindow is seeded from the offset:
With a negative
yOffset,lastVisibleRowIndex+1+btoi(hasHeaders)is negative,so
r.rowHeights[...]indexes out of range and panics. Large positive offsetsare already handled gracefully (the loop bails and the function returns early),
but the lower bound was never guarded.
Why it matters
YOffsetis the public scroll API for tables. In an interactive TUI the offsetis commonly driven directly by user key events. A naive
offset--on a scroll-upwithout clamping at 0 produces a negative offset, and the next render takes down
the whole program. A single out-of-range value turns a rendering call into a hard
crash (denial of service).
Reproduction:
Fix
Clamp negative offsets to 0 in the
YOffsetsetter, mirroring the existingclamping in the
TabWidthsetter. A negative scroll offset has no meaning (youcannot scroll above the first row), so 0 is the correct floor. This keeps the
invalid state from ever reaching the resizer.
Test
Before: the reproduction above panics with
index out of range [-2].After:
Render()returns normally. The full./table/test suite passes,including the existing
YOffsettests (which use positive offsets and areunaffected).