Skip to content

fix(explore): avoid panic when printing an empty result set - #68

Open
thegoodengineer wants to merge 1 commit into
openai:mainfrom
thegoodengineer:fix/explore-print-empty-panic
Open

fix(explore): avoid panic when printing an empty result set#68
thegoodengineer wants to merge 1 commit into
openai:mainfrom
thegoodengineer:fix/explore-print-empty-panic

Conversation

@thegoodengineer

Copy link
Copy Markdown

Summary

Pressing p ("print and exit") in the --format explore viewer panics when the result set is empty.

getSelectedContent indexes rowData at 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 TableView with no rows, table.Cursor() returns 0, and the index panics.

navigateForward already guards this exact case (added in TestNavigateForward_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. ExploreJSONStream collects zero items, marshalItemsToJSONArray returns [], and newTableView builds 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:

openai files list --format explore

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 via ExploreJSON.

Every other key binding (, , , , r, q) already handles the empty view fine. p is the only one that crashes.

Reproduction

Reverting just the one line change and running the test added here:

--- FAIL: TestGetSelectedContent_EmptyRowData (0.00s)
    --- FAIL: TestGetSelectedContent_EmptyRowData/[] (0.00s)
        Error: func (assert.PanicTestFunc) should not panic
            Panic value: runtime error: index out of range [0] with length 0
            Panic stack:
            github.com/openai/openai-cli/internal/jsonview.(*JSONViewer).getSelectedContent
                internal/jsonview/explorer.go:429
            github.com/openai/openai-cli/internal/jsonview.(*JSONViewer).Update
                internal/jsonview/explorer.go:415

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 p prints [] or {}. That matches the existing fallback for non table views a few lines above, which returns GetData().Raw.

The test drives Update with the actual p key 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

  • No behavior change for non empty views: the cursor is always in range there, so the existing branch is taken unchanged.
  • go build ./... and go vet ./... are clean; go test ./internal/... passes apart from internal/autocomplete, which fails identically on an unmodified checkout in my environment (it shells out to /bin/bash and I am on Windows), so it is unrelated to this change.

`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.
@thegoodengineer
thegoodengineer requested a review from a team as a code owner August 17, 2026 20:13
@markstuart-oai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-06T01:30:39.530284Z 71dad79 Manual request
🔒 Security Review Completed 2026-09-06T01:33:37.278349Z 71dad79 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 71dad7992b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Security review completed. No security issues were found in this pull request.

Reviewed commit: 71dad7992b

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants