fix(table): clamp cursor to 0 when SetRows is called with empty slice#1003
Open
LeSingh1 wants to merge 1 commit into
Open
fix(table): clamp cursor to 0 when SetRows is called with empty slice#1003LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
When SetRows was called with an empty rows slice, len(m.rows)-1 evaluated to -1, causing cursor to be set to -1. Subsequent calls to Cursor() would return -1, which is out of bounds and breaks callers that expect a valid index. The fix guards for the empty case and resets cursor to 0, consistent with the initial state of a new table. The existing shrink-to-fewer-rows guard is preserved for the non-empty case. Adds a regression test covering both the empty-rows reset and the previously-tested shrink case.
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.
When SetRows is called with an empty rows slice, the guard added in #783
still runs the assignment on line 310:
Because len(m.rows)-1 is -1 when the slice is empty, any cursor position
(including 0) satisfies the condition and gets set to -1. After that,
Cursor() returns -1 indefinitely until new rows are loaded.
The fix adds an explicit empty-slice guard that resets cursor to 0, which
matches the initial state of a freshly constructed table. The existing
shrink-to-fewer-rows path is unchanged.
Before this change, TestSetRows_CursorClampsToZeroOnEmpty failed:
After: both new tests pass and the full table suite stays green.