Skip to content

nil pointer dereference for null.String with some text values #689

@s-lukas

Description

@s-lukas

Describe the bug
panic: runtime error: invalid memory address or nil pointer dereference when running yaml.Marshal(...)

To Reproduce
This causes the failure:

import (
	"github.com/goccy/go-yaml"
	"gopkg.in/guregu/null.v4"
)

type MyObj struct {
	Test null.String `yaml:"test"`
}

func main() {
	obj := &MyObj{
		Test: null.StringFrom("#/components/schemas/ResponseBody"),
	}

	yaml.Marshal(obj)
}

But this works (only other string)

import (
	"github.com/goccy/go-yaml"
	"gopkg.in/guregu/null.v4"
)

type MyObj struct {
	Test null.String `yaml:"test"`
}

func main() {
	obj := &MyObj{
		Test: null.StringFrom("ResponseBody"),
	}

	yaml.Marshal(obj)
}

Expected behavior
Should always return a valid encoded yaml (or return an error if its invalid input)

Screenshots

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

goroutine 50 [running]:
github.com/goccy/go-yaml.(*Encoder).encodeStruct(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa724c0?, 0xc000088200?, 0x16?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:846 +0x5df
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa724c0?, 0xc000088200?, 0xa03ec0?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:490 +0x68a
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0x9dfee0?, 0xc000088200?, 0x98?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:464 +0x43e
github.com/goccy/go-yaml.(*Encoder).encodeMap(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44d00?, 0xc0000881f8?, 0xa44d00?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:699 +0x477
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44d00?, 0xc0000881f8?, 0xa44d00?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:495 +0x3cb
github.com/goccy/go-yaml.(*Encoder).encodeStruct(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa72560?, 0xc0000881e0?, 0x16?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:837 +0x552
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa72560?, 0xc0000881e0?, 0x16?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:490 +0x68a
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0x9dff20?, 0xc0000881e0?, 0x82?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:464 +0x43e
github.com/goccy/go-yaml.(*Encoder).encodeMap(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44c00?, 0xc000490268?, 0xa44c00?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:699 +0x477
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44c00?, 0xc000490268?, 0xa44c00?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:495 +0x3cb
github.com/goccy/go-yaml.(*Encoder).encodeStruct(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa72600?, 0xc000490258?, 0x16?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:837 +0x552
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa72600?, 0xc000490258?, 0xa03ec0?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:490 +0x68a
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0x9dffa0?, 0xc000490258?, 0x98?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:464 +0x43e
github.com/goccy/go-yaml.(*Encoder).encodeMap(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44b80?, 0xc0004949f0?, 0xa03ec0?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:699 +0x477
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44b80?, 0xc0004949f0?, 0x98?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:495 +0x3cb
github.com/goccy/go-yaml.(*Encoder).encodeMap(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44e00?, 0xc0004a4170?, 0xa44e00?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:699 +0x477
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xa44e00?, 0xc0004a4170?, 0xa44e00?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:495 +0x3cb
github.com/goccy/go-yaml.(*Encoder).encodeStruct(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xaa90e0?, 0xc0004a4140?, 0x16?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:837 +0x552
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0xaa90e0?, 0xc0004a4140?, 0xc00006c808?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:490 +0x68a
github.com/goccy/go-yaml.(*Encoder).encodeValue(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0x9e00a0?, 0xc0004a4140?, 0x7505e1c505c0?}, 0x1)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:464 +0x43e
github.com/goccy/go-yaml.(*Encoder).EncodeToNodeContext(0xc0004a3220, {0xc14ed0, 0x10acf40}, {0x9e00a0, 0xc0004a4140})
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:126 +0x231
github.com/goccy/go-yaml.(*Encoder).EncodeContext(0xc0004a3220, {0xc14ed0?, 0x10acf40?}, {0x9e00a0?, 0xc0004a4140?})
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/encode.go:89 +0x30
github.com/goccy/go-yaml.MarshalContext({0xc14ed0, 0x10acf40}, {0x9e00a0, 0xc0004a4140}, {0x0, 0x0, 0x0})
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/yaml.go:149 +0x1ad
github.com/goccy/go-yaml.MarshalWithOptions(...)
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/yaml.go:143
github.com/goccy/go-yaml.Marshal({0x9e00a0?, 0xc0004a4140?})
	/go)/pkg/mod/github.com/goccy/go-yaml@v1.17.1/yaml.go:138 +0x39

Version Variables

  • Go version: go1.24.1 linux/amd64
  • go-yaml's Version: v1.17.1

Additional context

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions