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

Codec deserialize into an invalid struct #29

Closed
StephenButtolph opened this issue Mar 25, 2020 · 0 comments
Closed

Codec deserialize into an invalid struct #29

StephenButtolph opened this issue Mar 25, 2020 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@StephenButtolph
Copy link
Contributor

Describe the bug
If the codec attempts to unmarshal a struct into an interface field that the struct does not implement, the codec will panic rather than returning an error.

To Reproduce
This test case, when placed in the Codec folder, will fail.

type outerInterface interface {
	ToInt() int
}

type outer struct {
	Interface outerInterface `serialize:"true"`
}

type innerInterface struct{}

func (it *innerInterface) ToInt() int {
	return 0
}

type innerNoInterface struct{}

// Ensure deserializing structs into the wrong interface errors gracefully
func TestUnmarshalInvalidInterface(t *testing.T) {
	codec := NewDefault()

	codec.RegisterType(&innerInterface{})
	codec.RegisterType(&innerNoInterface{})

	{
		bytes := []byte{0, 0, 0, 0}
		s := outer{}
		if err := codec.Unmarshal(bytes, &s); err != nil {
			t.Fatal(err)
		}
	}
	{
		bytes := []byte{0, 0, 0, 1}
		s := outer{}
		if err := codec.Unmarshal(bytes, &s); err == nil {
			t.Fatalf("should have errored")
		}
	}
}

Expected behavior
The above test should pass.

Operating System
Universally applicable

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

2 participants