Skip to content

Commit dd9bff0

Browse files
committed
cue/parser: added fuzzing code and fix one issue
The generated corpus is not included. Change-Id: I59afb5c4a28b41e3249ad2b4156a4bd2f1b67543 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2622 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 55b58eb commit dd9bff0

File tree

9 files changed

+101
-2
lines changed

9 files changed

+101
-2
lines changed

cue/parser/corpus/+commas.cue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package foo
2+
3+
import "path/to/pkg"
4+
import name "path/to/pkg"
5+
import . "path/to/pkg"
6+
import /* ERROR "expected 'STRING', found newline" */
7+
import err /* ERROR "expected 'STRING', found newline" */
8+
9+
foo: [
10+
0 // legal JSON
11+
]
12+
13+
bar: [
14+
0,
15+
1,
16+
2,
17+
3
18+
]

cue/parser/corpus/+comp.cue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"\(x)": y for x, y in {a: 1, b: 2}
2+
3+
z: [ x for x in [1, 2, 3] ]

cue/parser/corpus/+data.cue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package hello
2+
3+
who: "World"

cue/parser/corpus/+hello.cue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package hello
2+
3+
command echo: {
4+
task echo: {
5+
kind: "exec"
6+
cmd: "echo \(message)"
7+
stdout: string
8+
}
9+
10+
task display: {
11+
kind: "print"
12+
text: task.echo.stdout
13+
}
14+
}

cue/parser/corpus/+import.cue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package foo
2+
3+
import (
4+
"time.com/now"
5+
)
6+
7+
8+
foo: {
9+
bar: 3.4
10+
}
11+
12+
a b c: [1, 2Gi, 3M]

cue/parser/corpus/+test.cue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import "math"
2+
3+
foo: 1
4+
bar: "baz"

cue/parser/error_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,17 @@ func TestErrors(t *testing.T) {
182182
}
183183
}
184184
}
185+
186+
func TestFuzz(t *testing.T) {
187+
testCases := []string{
188+
"(({\"\\(0)\"(",
189+
"{{\"\\(0\xbf\"(",
190+
"a:y for x n{b:\"\"(\"\\(" +
191+
"\"\"\\\"(",
192+
}
193+
for _, tc := range testCases {
194+
t.Run("", func(t *testing.T) {
195+
_, _ = ParseFile("go-fuzz", []byte(tc))
196+
})
197+
}
198+
}

cue/parser/fuzz.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2019 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build gofuzz
16+
17+
package parser
18+
19+
func Fuzz(b []byte) int {
20+
_, err := ParseFile("go-fuzz", b)
21+
if err != nil {
22+
return 0
23+
}
24+
return 1
25+
}

cue/parser/parser.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ func (p *parser) closeList() {
147147
switch c.isList--; {
148148
case c.isList < 0:
149149
if !p.panicking {
150-
panic("unmatched close list")
150+
err := errors.Newf(p.pos, "unmatched close list")
151+
p.errors = errors.Append(p.errors, err)
152+
p.panicking = true
153+
panic(err)
151154
}
152155
case c.isList == 0:
153156
parent := c.parent
@@ -160,7 +163,10 @@ func (p *parser) closeList() {
160163
func (c *commentState) closeNode(p *parser, n ast.Node) ast.Node {
161164
if p.comments != c {
162165
if !p.panicking {
163-
panic("unmatched comments")
166+
err := errors.Newf(p.pos, "unmatched comments")
167+
p.errors = errors.Append(p.errors, err)
168+
p.panicking = true
169+
panic(err)
164170
}
165171
return n
166172
}

0 commit comments

Comments
 (0)