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

Return existing builder from PathBuilder.Root() instead of creating a new one. #420

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tobb10001
Copy link

  • Describe the purpose for which you created this PR.

This PR fixes a bug that I came across. It is triggered by a piece of code like this:

package main

import (
	"fmt"

	yaml "github.com/goccy/go-yaml"
)

func main() {
	builder := yaml.PathBuilder{}

	builder.Root()
	builder.Child("a")
	builder.Child("b")
	builder.Index(0)

	path := builder.Build()

	fmt.Println(path.String())
}

On the current master this code causes a panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x481ff8]

goroutine 1 [running]:
github.com/goccy/go-yaml.(*PathBuilder).child(...)
        /home/tobi/git/oss/go-yaml/path.go:417
github.com/goccy/go-yaml.(*PathBuilder).Child(0xc00011cf18, {0x4bd7e8?, 0xc00011cf30?})
        /home/tobi/git/oss/go-yaml/path.go:423 +0xb8
main.main()
        /home/tobi/git/oss/go-yaml/tmp/bug.go:13 +0x45
exit status 2

The root cause is that the PathBuilder.Root() method instanciates a new PathBuilder instance instead of mutating and returning the reciever. This is inconsistent with the similar methods IndexAll, Recursive, Index and Child and with my understanding of the builder pattern.

The given code sample assumes that the .Root() call mutates the builder, but it doesn't. Therefore the resulting path built by builder.Build() does not have a root. This in turn causes the panic when calling path.String().

This is not caught by the existing tests, because the existing tests use method chaining, which executes the next call on the builder returned by builder.Root(), which is a builder that has a root and therefore works as expected.

The fact that path.String() causes a panic in this case might be a separate issue that I didn't look into further.

  • Create test code that corresponds to the modification

@codecov-commenter
Copy link

codecov-commenter commented Jan 15, 2024

Codecov Report

Merging #420 (fdcc8ba) into master (0640a15) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #420      +/-   ##
==========================================
+ Coverage   76.02%   76.03%   +0.01%     
==========================================
  Files          13       13              
  Lines        4692     4694       +2     
==========================================
+ Hits         3567     3569       +2     
  Misses        866      866              
  Partials      259      259              

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

Successfully merging this pull request may close these issues.

3 participants