Skip to content

Commit

Permalink
cmd/cue: do not panic on empty jsonl inputs
Browse files Browse the repository at this point in the history
buildPlan.instances always returned an iterator with at least
one instance, and in the case of an empty jsonl file,
this would result in iterating over a single nil instance.
This would cause the following logic, like exporting, to panic.

An empty jsonl input means zero files, and is allowed by the spec.
Moreover, zero bytes is not valid input, unlike YAML or TOML,
which produce null and {} respectively.
For these reasons, treat empty jsonl inputs as a stream of zero values.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Ie0a62bb59d347011de5aa47f5ee6d175824451a3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199451
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mvdan committed Aug 14, 2024
1 parent 8b3cc67 commit 93670d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 7 additions & 1 deletion cmd/cue/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,18 @@ func (b *buildPlan) instances() iterator {
e: err,
i: -1,
}
default:
case b.instance != nil:
i = &instanceIterator{
a: []*instance{b.instance},
i: -1,
}
b.instance = nil
default:
// No instances; return an iterator with zero values.
// This can happen when the input is an empty jsonl file, for example.
// Zero iteration may not make much sense in some scenarios like export,
// but an empty jsonl file is valid, so doing nothing seems reasonable.
i = &instanceIterator{}
}
if len(b.expressions) > 0 {
return &expressionIter{
Expand Down
14 changes: 8 additions & 6 deletions cmd/cue/cmd/testdata/script/encoding_empty.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Test that the various encodings cope with empty files correctly.

# TODO(mvdan): fix the panics below; see https://cuelang.org/issue/1790 and https://cuelang.org/issue/2714.
# Note that empty files or inputs have different meanings per encoding.
# For example, in CUE and TOML they mean an empty struct or object,
# in JSON they are invalid, and in JSONL they are a stream of zero values.

# TODO(mvdan): cover more encodings: jsonschema, openapi, textproto, proto.

Expand All @@ -12,8 +13,8 @@ exec cue export cue: empty
cmp stdout as-cue.stdout
! exec cue export json: empty
stderr 'unexpected end of JSON input'
! exec cue export jsonl: empty
stderr '^panic: '
exec cue export jsonl: empty
cmp stdout as-jsonl.stdout
exec cue export yaml: empty
cmp stdout as-yaml.stdout
exec cue export toml: empty
Expand All @@ -25,8 +26,8 @@ exec cue export cue: newlines
cmp stdout as-cue.stdout
! exec cue export json: newlines
stderr 'unexpected end of JSON input'
! exec cue export jsonl: newlines
stderr '^panic: '
exec cue export jsonl: newlines
cmp stdout as-jsonl.stdout
exec cue export yaml: newlines
cmp stdout as-yaml.stdout
exec cue export toml: newlines
Expand All @@ -48,6 +49,7 @@ cmp stdout as-toml.stdout

-- as-cue.stdout --
{}
-- as-jsonl.stdout --
-- as-yaml.stdout --
null
-- as-toml.stdout --
Expand Down

0 comments on commit 93670d7

Please sign in to comment.