Skip to content

panic on encoding embedded structs - encoder/vm.ptrToUint64 #552

@sirgwain

Description

@sirgwain

I'm encountering a panic with embedded structs. I have been able to reproduce it in a tiny example.

package main

import (
	"bytes"

	"github.com/goccy/go-json"
)

var testJson = `{"id": 1}`

type DBObject struct {
	ID int64 `json:"id,omitempty"`
}

type Item struct {
	DBObject
}

type request struct {
	*Item
}

func main() {
	var req request
	if err := json.Unmarshal([]byte(testJson), &req); err != nil {
		panic(err)
	}

	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	if err := enc.EncodeWithOption(req, json.Debug()); err != nil {
		panic(err)
	}
}

Output:

=============[DEBUG]===============
* [TYPE]
main.request

* [ALL OPCODE]
[000]-StructHead ([idx:0][key:Item][offset:0])
[001]-StructPtrHead ([idx:1][key:DBObject][offset:0])
[002]-StructHeadOmitEmptyInt ([idx:2][key:id][offset:0])
[003]-StructEnd ([idx:3][key:][offset:0])

* [CONTEXT]
&{Context:<nil> Buf:[] MarshalBuf:[] Ptrs:[1374389592464 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] KeepRefs:[0x1400000e190] SeenPtr:[] BaseIndent:0 Prefix:[] IndentStr:[] Option:0x1400010a180}
===================================
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=0x1 pc=0x10471701c]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder/vm.DebugRun.func1()
	/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/debug_vm.go:36 +0x2a4
panic({0x10479c2e0?, 0x1048b2510?})
	/opt/homebrew/Cellar/go/1.24.2/libexec/src/runtime/panic.go:792 +0x124
github.com/goccy/go-json/internal/encoder/vm.ptrToUint64(0x1, 0x0?)
	/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/util.go:80 +0x5c
github.com/goccy/go-json/internal/encoder/vm.Run(0x1400010c750, {0x14000142000?, 0x0?, 0x400?}, 0x1400011e070?)
	/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/vm.go:646 +0x2994
github.com/goccy/go-json/internal/encoder/vm.DebugRun(0xfadc9882eba6831f?, {0x14000142000?, 0x0?, 0x0?}, 0x0?)
	/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/debug_vm.go:40 +0x5c
github.com/goccy/go-json.encodeRunCode(0x1400010c750?, {0x14000142000?, 0x1047a8901?, 0x0?}, 0x1400013dda8?)
	/go/pkg/mod/github.com/goccy/[email protected]/encode.go:305 +0x40
github.com/goccy/go-json.encode(0x1400010c750, {0x10479f7a0, 0x1400000e190})
	/go/pkg/mod/github.com/goccy/[email protected]/encode.go:235 +0x204
github.com/goccy/go-json.(*Encoder).encodeWithOption(0x1400015bef0, 0x1400010c750, {0x10479f7a0, 0x1400000e190}, {0x1400013df28, 0x1, 0x28?})
	/go/pkg/mod/github.com/goccy/[email protected]/encode.go:77 +0x120
github.com/goccy/go-json.(*Encoder).EncodeWithOption(0x1400013def0, {0x10479f7a0, 0x1400000e190}, {0x1400013df28, 0x1, 0x1})
	/go/pkg/mod/github.com/goccy/[email protected]/encode.go:42 +0x78
main.main()
	/temp/go-json-test/main.go:31 +0xdc
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions