Skip to content

Invalid yaml can result in an errors.SyntaxError.Error() call which panics #718

@brackendawson

Description

@brackendawson

Describe the bug
Some invalid yaml can result in a SyntaxError instance which panics when you call Error().

To Reproduce
I've modified a fuzz test to demonstrate the panic: brackendawson@09c26ee

--- FAIL: FuzzUnmarshalToMap (0.01s)
    --- FAIL: FuzzUnmarshalToMap/seed#96 (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x100753a88]

goroutine 1210 [running]:
testing.tRunner.func1.2({0x100931960, 0x100b6cd50})
	/usr/local/go/src/testing/testing.go:1734 +0x2bc
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1737 +0x47c
panic({0x100931960?, 0x100b6cd50?})
	/usr/local/go/src/runtime/panic.go:792 +0x124
github.com/goccy/go-yaml/internal/errors.FormatError({0x100859114, 0x1f}, 0x0, 0x0, 0x1)
	/Users/bracken/src/githib.com/goccy/go-yaml/internal/errors/error.go:237 +0x58
github.com/goccy/go-yaml/internal/errors.(*SyntaxError).FormatError(...)
	/Users/bracken/src/githib.com/goccy/go-yaml/internal/errors/error.go:137
github.com/goccy/go-yaml/internal/errors.(*SyntaxError).Error(0xc000095950)
	/Users/bracken/src/githib.com/goccy/go-yaml/internal/errors/error.go:133 +0x70
github.com/goccy/go-yaml_test.FuzzUnmarshalToMap.func1(0xc0004c01c0, {0xc0002ac7c0, 0xd, 0x10})
	/Users/bracken/src/githib.com/goccy/go-yaml/fuzz_test.go:54 +0x178
reflect.Value.call({0x100924d00?, 0x100987828?, 0x13?}, {0x10084d1b7, 0x4}, {0xc0003ab8c0, 0x2, 0x2?})
	/usr/local/go/src/reflect/value.go:584 +0x938
reflect.Value.Call({0x100924d00?, 0x100987828?, 0x1?}, {0xc0003ab8c0, 0x2, 0x2})
	/usr/local/go/src/reflect/value.go:368 +0x94
testing.(*F).Fuzz.func1.1(0xc0004c01c0)
	/usr/local/go/src/testing/fuzz.go:340 +0x310
testing.tRunner(0xc0004c01c0, 0xc00015a6c0)
	/usr/local/go/src/testing/testing.go:1792 +0x184
created by testing.(*F).Fuzz.func1 in goroutine 1113
	/usr/local/go/src/testing/fuzz.go:327 +0x8ac
FAIL	github.com/goccy/go-yaml	0.539s
FAIL

In short, this will panic:

var v map[string]any
err := yaml.Unmarshal("{\"000\":0000A,", &v)
if err != nil {
    err.Error() # panic here
}

Expected behavior
Errors returned by go-yaml do not panic

Screenshots
Errors returned by go-yaml can panic

Version Variables

  • Go version: 1.24.1
  • go-yaml's Version: master@9d0a7f2

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions