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

Differing behaviour on UnmarshalYAML interfaces #568

Open
0xTylerHolmes opened this issue Dec 4, 2024 · 0 comments
Open

Differing behaviour on UnmarshalYAML interfaces #568

0xTylerHolmes opened this issue Dec 4, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@0xTylerHolmes
Copy link

Describe the bug
When we implement the unmarshal interface for an object we don't call it when unmarshalling the object depending on how we perform the call. This can lead to panics in the decoder on malformed input.

To Reproduce
playground-link

type FooJSON struct {
	Foo uint64 `json:"foo"`
}

type FooYAML struct {
	Foo uint64 `yaml:"foo"`
}

func (s *FooJSON) UnmarshalYAML(input []byte) error {
	var data FooYAML
	if err := yaml.Unmarshal(input, &data); err != nil {
		return err
	}
	s.Foo = data.Foo
	return nil
}

func TestWrappedJSONDecode(t *testing.T) {
	var exampleStruct FooJSON
	testCase := "0: &\xeb\r&:\r-\x00\r*\xfb\r:\n!\r*\xeb\r:\n!\n!\r-\n!\r*\r::"
	// succeeds
	_ = exampleStruct.UnmarshalYAML([]byte(testCase))
	// panics
	_ = yaml.Unmarshal([]byte(testCase), &exampleStruct)
}

Expected behavior
I would expect .UnmarshalYAML() to have the same behavior as the yaml.Unmarshal(, &) when obj implements the UnmarshalYAML method.

Screenshots
N/A

Version Variables
Tested on:

  • Go version: [1.23 ]
  • go-yaml's Version: [latest ]

Additional context
N/A

@0xTylerHolmes 0xTylerHolmes added the bug Something isn't working label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant