Skip to content

fix(cmdutil): return converted YAML bytes directly in formatJSON and prevent nil-pointer panic - #33

Open
Adityakk9031 wants to merge 1 commit into
context-dot-dev:mainfrom
Adityakk9031:fix/yaml-format-panic-and-stream
Open

Adityakk9031 wants to merge 1 commit into
context-dot-dev:mainfrom
Adityakk9031:fix/yaml-format-panic-and-stream

Conversation

@Adityakk9031

Copy link
Copy Markdown

Summary

In \pkg/cmd/cmdutil.go\ (\ ormatJSON), when --format yaml\ was requested, the function was writing directly to \opts.Stdout\ and returning (nil, err).

This caused several issues:

  1. Nil-pointer dereference panic: If \opts.Stdout\ was nil (e.g. when callers or tests invoked \ ormatJSON\ without defaults initialized), calling \opts.Stdout.Write(...)\ triggered a panic.
  2. Empty byte slice returned: Callers received an empty []byte, causing callers to lose the formatted output.
  3. Broken pagination and streaming: In \ShowJSONIterator, \ ormatJSON\ prematurely wrote output to stdout before terminal height calculations and pager setup, resulting in corrupted line counts and empty output streamed to pagers.

Solution

Return []byte(yaml.String()), nil\ directly from \ ormatJSON\ in the YAML case, consistent with all other formats (\json, \jsonl,
aw, \pretty). \ShowJSON\ and \ShowJSONIterator\ then handle writing/buffering the returned bytes as intended.

Tests

  • Added unit test in \pkg/cmd/cmdutil_test.go\ (\YAMLFormat) verifying that \ ormatJSON\ successfully converts JSON to YAML and does not panic when \opts.Stdout\ is nil.

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.

1 participant