Skip to content

Commit

Permalink
fix invalid path (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Dec 18, 2024
1 parent 7d56fe2 commit 1375813
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ end:
}

func parsePathDot(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) {
if b.root == nil || b.node == nil {
return nil, nil, 0, fmt.Errorf("required '$' character at first: %w", ErrInvalidPathString)
}
length := len(buf)
if cursor+1 < length && buf[cursor+1] == '.' {
b, buf, c, err := parsePathRecursive(b, buf, cursor)
Expand Down Expand Up @@ -119,6 +122,10 @@ end:
}

func parseQuotedKey(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) {
if b.root == nil || b.node == nil {
return nil, nil, 0, fmt.Errorf("required '$' character at first: %w", ErrInvalidPathString)
}

cursor++ // skip single quote
start := cursor
length := len(buf)
Expand Down Expand Up @@ -156,6 +163,10 @@ end:
}

func parsePathIndex(b *PathBuilder, buf []rune, cursor int) (*PathBuilder, []rune, int, error) {
if b.root == nil || b.node == nil {
return nil, nil, 0, fmt.Errorf("required '$' character at first: %w", ErrInvalidPathString)
}

length := len(buf)
cursor++ // skip '[' character
if length <= cursor {
Expand Down
27 changes: 27 additions & 0 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,33 @@ building:
}
}

func TestInvalidPath(t *testing.T) {
tests := []struct {
name string
path string
}{
{
name: "missing root with dot",
path: ".foo",
},
{
name: "missing root with index",
path: "foo[0]",
},
{
name: "missing root with recursive",
path: "..foo",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if _, err := yaml.PathString(test.path); err == nil {
t.Fatal("expected error")
}
})
}
}

func ExamplePath_AnnotateSource() {
yml := `
a: 1
Expand Down

0 comments on commit 1375813

Please sign in to comment.