Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic on printing some YAML as JSON #1425

Closed
haguenau opened this issue Nov 10, 2022 · 3 comments
Closed

Panic on printing some YAML as JSON #1425

haguenau opened this issue Nov 10, 2022 · 3 comments
Labels

Comments

@haguenau
Copy link

Describe the bug

Some YAML documents cause yq to panic. I would expect either a clear error message (assuming input is invalid), or no error at all (assuming input is valid).

Version of yq: 4.29.2
Operating system: macOS
Installed via: binary release

Input Yaml

input.yml:

---
a: &a
  - 0

b: &b
  - 1

c:
  <<: [*a, *b]

Command
The command you ran:

yq-4.29.2 . <in.yml -o json

Actual behavior

panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
github.com/mikefarah/yq/v4/pkg/yqlib.applyAlias(0xc000287310?, 0xc000354e60, 0x1486786?, {0xc00034e780, 0xc00034e7e0, 0x0, {0x0, 0x0}})
	/home/runner/work/yq/yq/pkg/yqlib/operator_anchors_aliases.go:250 +0x1b3
github.com/mikefarah/yq/v4/pkg/yqlib.reconstructAliasedMap(0xc000355040, {0xc00034e660, 0x0, 0x0, {0x0, 0x0}})
	/home/runner/work/yq/yq/pkg/yqlib/operator_anchors_aliases.go:163 +0x599
github.com/mikefarah/yq/v4/pkg/yqlib.explodeNode(0xc000355040, {0xc00034e660, 0x0, 0x0, {0x0, 0x0}})
	/home/runner/work/yq/yq/pkg/yqlib/operator_anchors_aliases.go:222 +0x30c
github.com/mikefarah/yq/v4/pkg/yqlib.explodeNode(0xc000354b40, {0xc00034e660, 0x0, 0x0, {0x0, 0x0}})
	/home/runner/work/yq/yq/pkg/yqlib/operator_anchors_aliases.go:232 +0x3d0
github.com/mikefarah/yq/v4/pkg/yqlib.explodeNode(0xc000200780?, {0xc00034e660, 0x0, 0x0, {0x0, 0x0}})
	/home/runner/work/yq/yq/pkg/yqlib/operator_anchors_aliases.go:192 +0x29b
github.com/mikefarah/yq/v4/pkg/yqlib.explodeOperator(0xc000200780?, {0xc00034e660, 0x0, 0x0, {0x0, 0x0}}, 0xc0002143c0)
	/home/runner/work/yq/yq/pkg/yqlib/operator_anchors_aliases.go:133 +0x2fe
github.com/mikefarah/yq/v4/pkg/yqlib.(*dataTreeNavigator).GetMatchingNodes(0xc000200780?, {0xc00034e660, 0x0, 0x0, {0x0, 0x0}}, 0xc0002143c0)
	/home/runner/work/yq/yq/pkg/yqlib/data_tree_navigator.go:37 +0x25c
github.com/mikefarah/yq/v4/pkg/yqlib.(*resultsPrinter).PrintResults(0xc0002064e0, 0xc00034e660)
	/home/runner/work/yq/yq/pkg/yqlib/printer.go:96 +0x203
github.com/mikefarah/yq/v4/pkg/yqlib.(*streamEvaluator).Evaluate(0xc000287c90, {0x1479d0d, 0x1}, {0x1546c20?, 0xc0002065a0?}, 0x1f53308?, {0x1548f78, 0xc0002064e0}, {0x1548210, 0xc00034e450})
	/home/runner/work/yq/yq/pkg/yqlib/stream_evaluator.go:112 +0x339
github.com/mikefarah/yq/v4/pkg/yqlib.(*streamEvaluator).EvaluateFiles(0xc0000a0008?, {0x7ff7bfefefd2, 0x1}, {0xc000226c10, 0x1, 0xc00021e500?}, {0x1548f78, 0xc0002064e0}, {0x1548210, 0xc00034e450})
	/home/runner/work/yq/yq/pkg/yqlib/stream_evaluator.go:65 +0x1af
github.com/mikefarah/yq/v4/cmd.evaluateSequence(0xc00022c300?, {0xc000226c00, 0x1, 0x3})
	/home/runner/work/yq/yq/cmd/evalute_sequence_command.go:132 +0x899
github.com/spf13/cobra.(*Command).execute(0xc00022c300, {0xc000226ba0, 0x3, 0x3})
	/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:916 +0x862
github.com/spf13/cobra.(*Command).ExecuteC(0xc00022c000)
	/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 +0x3bd
github.com/spf13/cobra.(*Command).Execute(...)
	/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968
main.main()
	/home/runner/work/yq/yq/yq.go:22 +0x1e5

Expected behavior

{
  "a": [
    0
  ],
  "b": [
    1
  ],
  "c": [
    0,
    1
  ]
}

Additional context
I don't have a strong opinion on whether the input YAML is valid. Note that if YAML output is requested, no panic occurs; instead, slightly different YAML text is emitted:

---
a: &a
  - 0
b: &b
  - 1
c:
  !!merge <<: [*a, *b]

This document also causes a panic with yq -o json.

@mikefarah
Copy link
Owner

Oh I see - this is a problem with the yaml, merge anchors only work with maps and both 'a' and 'b' are sequences. I will update yq to return a proper error message.

mikefarah added a commit that referenced this issue Nov 11, 2022
@mikefarah
Copy link
Owner

Fixed in 4.30.1

@haguenau
Copy link
Author

Thanks for the impressively quick fix! Indeed, I get a proper error message this morning on Homebrew version 4.30.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants