goccy / go-json

Fast JSON encoder/decoder compatible with encoding/json for Go
MIT License
3.12k stars 148 forks source link

[BUG] Fatal error: invalid pointer found on stack #504

Closed viewsharp closed 6 months ago

viewsharp commented 7 months ago

When executing the following code:

package main

import (
    "testing"

    "github.com/goccy/go-json"
    "github.com/stretchr/testify/require"
)

type CustomMap map[string]any

func (m CustomMap) MarshalJSON() ([]byte, error) {
    if m == nil {
        return nil, nil
    }
    t := (map[string]interface{})(m)
    return json.Marshal(t)
}

func TestCustomMap(t *testing.T) {
    jsonData, err := json.Marshal([]CustomMap{
        {"foo": "bar"},
    })
    require.NoError(t, err)
    require.Equal(t, []byte(`[{"foo":"bar"}]`), jsonData)
}

The following error occurs:

runtime: bad pointer in frame github.com/goccy/go-json/internal/encoder/vm.Run at 0x14000161f90: 0x1
fatal error: invalid pointer found on stack

Runtime stack: runtime_stack.log

Go version: go version go1.20.4 darwin/arm64