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

validation crash on missing nested required field #146

Closed
itaischwartz opened this issue Jun 28, 2020 · 0 comments · Fixed by #147
Closed

validation crash on missing nested required field #146

itaischwartz opened this issue Jun 28, 2020 · 0 comments · Fixed by #147
Labels
bug Something isn't working

Comments

@itaischwartz
Copy link
Contributor

Hey @goccy,

I've run the following test:

package main 

import (
	"strings"

	"github.com/goccy/go-yaml"
	"gopkg.in/go-playground/validator.v9"
)

type Person struct {
	Name string `yaml:"name" validate:"required"`
	Age  int    `yaml:"age" validate:"gte=0,lt=120"`
	Addr *PersonAddress `yaml:"addr" validate:"required,dive,required"`
}
type PersonAddress struct {
	Number string `yaml:"number" validate:"required"`
	State string `yaml:"state" validate:"required"`
}


func main() {
	yml := `
name: itai
age: 10
addr:
  number: seven
` // missing State
	validate := validator.New()
	dec := yaml.NewDecoder(
		strings.NewReader(yml),
		yaml.Validator(validate),
	)
	var v Person
	err := dec.Decode(&v)
	if err == nil {
		panic("expected error")
	}
}

got the following crash log:

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

goroutine 1 [running]:
github.com/goccy/go-yaml.(*Decoder).decodeStruct(0xc0001fe000, 0x11b2480, 0xc0000a9fc0, 0x199, 0x1210c20, 0xc000105a80, 0x28, 0x11aa560)
        /Users/itai/work/go-yaml/decode.go:855 +0x1b12
github.com/goccy/go-yaml.(*Decoder).decodeValue(0xc0001fe000, 0x11b2480, 0xc0000a9fc0, 0x199, 0x1210c20, 0xc000105a80, 0x0, 0x2a)
        /Users/itai/work/go-yaml/decode.go:473 +0x1b0a
github.com/goccy/go-yaml.(*Decoder).decode(0xc0001fe000, 0x1189e00, 0xc0000a9fc0, 0x16, 0x1189e00, 0x1337a20)
        /Users/itai/work/go-yaml/decode.go:1243 +0xd2
github.com/goccy/go-yaml.(*Decoder).Decode(0xc0001fe000, 0x1189e00, 0xc0000a9fc0, 0x1006cbf, 0xc00008e058)
        /Users/itai/work/go-yaml/decode.go:1272 +0x272
main.main()
        /Users/itati/work/go-yaml/cmd/validate/cmd.go:34 +0x1f6
exit status 2

shell returned 1

It seems that structFieldMap doesn't contain "State" fieldName, so we crash on null pointer deref (structField.RenderName)

					structField := structFieldMap[fieldName]
					node, exists := keyToNodeMap[structField.RenderName]
@goccy goccy added the bug Something isn't working label Jul 2, 2020
@goccy goccy closed this as completed in #147 Jul 3, 2020
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

Successfully merging a pull request may close this issue.

2 participants