Skip to content

Panic when putting a struct in the array of interface{} #285

@Songmu

Description

@Songmu

The following code will make it panic. I have checked with the latest version of go-json. 4f05809

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

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

func main() {
	if err := run(os.Args[1:]); err != nil {
		log.Fatal(err)
	}
}

func run(args []string) error {
	body := []interface{}{
		struct{}{},
	}
	var b bytes.Buffer
	if err := json.NewEncoder(&b).Encode(body); err != nil {
		return err
	}
	fmt.Println(b.String())
	return nil
}
$ go run ./main.go
unexpected fault address 0xc001398120
fatal error: fault
[signal SIGBUS: bus error code=0x2 addr=0xc001398120 pc=0x1125cfa]

goroutine 1 [running]:
runtime.throw({0x1174e22, 0xc0000d3400})
        /usr/local/Cellar/go/1.17/libexec/src/runtime/panic.go:1198 +0x71 fp=0xc0000e0870 sp=0xc0000e0840 pc=0x10305b1
runtime.sigpanic()
        /usr/local/Cellar/go/1.17/libexec/src/runtime/signal_unix.go:732 +0x1d6 fp=0xc0000e08c0 sp=0xc0000e0870 pc=0x1044156
github.com/goccy/go-json/internal/encoder/vm.load(...)
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/util.go:51
github.com/goccy/go-json/internal/encoder/vm.Run(0xc00009b110, {0xc0000ce000, 0x0, 0x400}, 0xc0000b0080)
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/vm.go:342 +0x17a fp=0xc0000e3d30 sp=0xc0000e08c0 pc=0x1125cfa
github.com/goccy/go-json.encodeRunCode(0x115f260, {0xc0000ce000, 0xc0000d0000, 0x0}, 0xc000098180)
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/encode.go:307 +0x68 fp=0xc0000e3d68 sp=0xc0000e3d30 pc=0x1156c48
github.com/goccy/go-json.encode(0xc00009b110, {0x115f260, 0xc0000a4048})
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/encode.go:232 +0x21c fp=0xc0000e3de8 sp=0xc0000e3d68 pc=0x115675c
github.com/goccy/go-json.(*Encoder).encodeWithOption(0xc0000e3f00, 0xc00009b110, {0x115f260, 0xc0000a4048}, {0x0, 0x0, 0xc000092ea0})
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/encode.go:74 +0xeb fp=0xc0000e3e48 sp=0xc0000e3de8 pc=0x115640b
github.com/goccy/go-json.(*Encoder).EncodeWithOption(0xc000096260, {0x115f260, 0xc0000a4048}, {0x0, 0x0, 0x0})
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/encode.go:41 +0x92 fp=0xc0000e3eb0 sp=0xc0000e3e48 pc=0x1156292
github.com/goccy/go-json.(*Encoder).Encode(...)
        /Users/Songmu/dev/pkg/mod/github.com/goccy/[email protected]/encode.go:33
main.run({0x0, 0x0, 0x1016ae5})
        /Users/Songmu/dev/src/.tmp/work/main.go:23 +0xc5 fp=0xc0000e3f48 sp=0xc0000e3eb0 pc=0x11572c5
main.main()
        /Users/Songmu/dev/src/.tmp/work/main.go:13 +0x54 fp=0xc0000e3f80 sp=0xc0000e3f48 pc=0x1157194
runtime.main()
        /usr/local/Cellar/go/1.17/libexec/src/runtime/proc.go:255 +0x227 fp=0xc0000e3fe0 sp=0xc0000e3f80 pc=0x1032c47
runtime.goexit()
        /usr/local/Cellar/go/1.17/libexec/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc0000e3fe8 sp=0xc0000e3fe0 pc=0x105c4c1
exit status 2

The standard encoding/json produces the following output.

[{}]

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