fix(explore): avoid panic when printing an empty result set - #68
Open
thegoodengineer wants to merge 1 commit into
Open
fix(explore): avoid panic when printing an empty result set#68thegoodengineer wants to merge 1 commit into
thegoodengineer wants to merge 1 commit into
Conversation
`getSelectedContent` indexed `rowData` at the table cursor without checking
that a row exists. An empty array or object builds a TableView with no rows,
so pressing "p" on one crashed the CLI with an index out of range panic.
This is reachable whenever a list endpoint returns no results, since
`ExploreJSONStream` marshals zero items into `[]` and builds the table from
that, e.g. `openai files list --format explore` on an account with no files.
`navigateForward` already guards the same empty `rowData` case; this applies
the equivalent check to the print path and falls back to the container that
the view is displaying, so "p" prints `[]` or `{}` instead of panicking.
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.
Summary
Pressing
p("print and exit") in the--format exploreviewer panics when the result set is empty.getSelectedContentindexesrowDataat the table cursor without checking that a row exists:https://github.com/openai/openai-cli/blob/7d87ee2/internal/jsonview/explorer.go#L423-L434
An empty array or object builds a
TableViewwith no rows,table.Cursor()returns0, and the index panics.navigateForwardalready guards this exact case (added inTestNavigateForward_EmptyRowData), so this looks like the same oversight in the sibling path rather than an intended difference.How it is reached
Any list endpoint that returns no results.
ExploreJSONStreamcollects zero items,marshalItemsToJSONArrayreturns[], andnewTableViewbuilds a table with no rows:https://github.com/openai/openai-cli/blob/7d87ee2/internal/jsonview/explorer.go#L331-L349
So on an account with no files:
then press
p, and the CLI exits with a runtime panic and a Go stack trace instead of printing anything. The same applies to a top level empty object viaExploreJSON.Every other key binding (
↑,↓,←,→,r,q) already handles the empty view fine.pis the only one that crashes.Reproduction
Reverting just the one line change and running the test added here:
With the fix applied, both cases pass.
Fix
Bounds check the cursor before indexing, and fall back to the container the view is already displaying, so
pprints[]or{}. That matches the existing fallback for non table views a few lines above, which returnsGetData().Raw.The test drives
Updatewith the actualpkey message rather than calling the unexported helper directly, so it covers the real key binding path and asserts the printed output, not just the absence of a panic.Notes
go build ./...andgo vet ./...are clean;go test ./internal/...passes apart frominternal/autocomplete, which fails identically on an unmodified checkout in my environment (it shells out to/bin/bashand I am on Windows), so it is unrelated to this change.