Repository: goccy/go-yaml
Pull request #598 (2024-12-18 01:41:24)

Fix invalid path
fix https://github.com/goccy/go-yaml/issues/595

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/598?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
Attention: Patch coverage is `33.33333%` with `6 lines` in your changes missing coverage. Please review.
> Project coverage is 77.35%. Comparing base [(`7d56fe2`)](https://app.codecov.io/gh/goccy/go-yaml/commit/7d56fe29f31ec88430452ac74bfe82e088099ec6?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`450d215`)](https://app.codecov.io/gh/goccy/go-yaml/commit/450d2150e55a2ee78e976bd6dda3b2675396155b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #598      +/-   ##
==========================================
- Coverage   77.37%   77.35%   -0.03%     
==========================================
  Files          21       21              
  Lines        7820     7829       +9     
==========================================
+ Hits         6051     6056       +5     
- Misses       1352     1354       +2     
- Partials      417      419       +2     
```

</details>

Patch:
diff --git a/path.go b/path.go
index fcb5681..aaa3477 100644
--- a/path.go
+++ b/path.go
@@ -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)
@@ -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)
@@ -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 {


Test patch:
diff --git a/path_test.go b/path_test.go
index 84e6cc0..5b62d26 100644
--- a/path_test.go
+++ b/path_test.go
@@ -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