Skip to content

jsoniter crash on custom map encoders #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sreekanth-cb opened this issue Dec 6, 2017 · 2 comments
Closed

jsoniter crash on custom map encoders #202

sreekanth-cb opened this issue Dec 6, 2017 · 2 comments

Comments

@sreekanth-cb
Copy link

Seeing a crash with custom map encoders with jsoniter. The same works fine with std json.
Attaching the code.

package main

import (
	"encoding/json"
	"fmt"
	"unsafe"

	jsoniter "github.com/json-iterator/go"
)

func jsoniterMarshal(input Status) {
	bb, err := jsoniter.Marshal(&input)
	if err != nil {
		fmt.Printf("JSONITER Error %v \n", err)
		return
	}
	fmt.Printf("JSONITER Res %s  \n", bb)
}

func jsonMarshal(input Status) {
	bb, err := json.Marshal(&input)
	if err != nil {
		fmt.Printf("JSON Error %v \n", err)
		return
	}
	fmt.Printf("JSON Res %s  \n", bb)
}

type ErrMap map[string]error

type Status struct {
	Total   int    `json:"total"`
	ErrMaps ErrMap `json:"errMaps,omitempty"`
}

func (iem ErrMap) MarshalJSON() ([]byte, error) {
	tmp := make(map[string]string, len(iem))
	for k, v := range iem {
		tmp[k] = v.Error()
	}
	return json.Marshal(tmp)
}

func encodeMap(ptr unsafe.Pointer, stream *jsoniter.Stream) {
	iem := *((*ErrMap)(ptr))
	//log.Printf("\nval-> %+v", iem)
	if iem != nil {
		tmp := make(map[string]string, len(iem))
		for k, v := range iem {
			tmp[k] = v.Error()
		}
		stream.WriteVal(tmp)
	}
}

func main() {
	jsoniter.RegisterTypeEncoderFunc("main.ErrMap", encodeMap, nil)

	eMap := make(ErrMap, 2)
	eMap["first"] = fmt.Errorf("First Error")
	eMap["second"] = fmt.Errorf("Second Error")
	s := Status{Total: 100,
		ErrMaps: eMap}

	jsonMarshal(s)
	jsoniterMarshal(s)

}
@sreekanth-cb sreekanth-cb changed the title crash on custom map encoders jsoniter crash on custom map encoders Dec 7, 2017
@taowen
Copy link
Contributor

taowen commented Dec 7, 2017

fixed by 2307887

@taowen taowen closed this as completed Dec 7, 2017
@sreekanth-cb
Copy link
Author

Whoops..I still see the same crash for the above program.
Can you please confirm this?
goroutine 1 [running]:
main.encodeMap(0xc42007c4e0, 0xc420074360)
/Users/sreekanth/jsoniter/mapFails.go:48 +0x48
github.com/json-iterator/go.(*funcEncoder).Encode(0xc420076250, 0xc42007c4e0, 0xc420074360)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect_extension.go:99 +0x3d
github.com/json-iterator/go.(*optionalMapEncoder).Encode(0xc4200764b0, 0xc4200763e8, 0xc420074360)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect.go:144 +0x4f
github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc420070420, 0xc4200763e0, 0xc420074360)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect_object.go:118 +0x5c
github.com/json-iterator/go.(*structEncoder).Encode(0xc4200704c0, 0xc4200763e0, 0xc420074360)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect_object.go:155 +0xc2
github.com/json-iterator/go.(*OptionalEncoder).Encode(0xc420076510, 0xc420076528, 0xc420074360)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect.go:124 +0x4f
github.com/json-iterator/go.WriteToStream(0x10fad60, 0xc4200763e0, 0xc420074360, 0x11c3c80, 0xc420076510)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect.go:47 +0xd5
github.com/json-iterator/go.(*OptionalEncoder).EncodeInterface(0xc420076510, 0x10fad60, 0xc4200763e0, 0xc420074360)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect.go:129 +0x55
github.com/json-iterator/go.(*Stream).WriteVal(0xc420074360, 0x10fad60, 0xc4200763e0)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_reflect.go:252 +0xe4
github.com/json-iterator/go.(*frozenConfig).Marshal(0xc420096100, 0x10fad60, 0xc4200763e0, 0x0, 0x0, 0x0, 0x0, 0x0)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_config.go:263 +0xdb
github.com/json-iterator/go.Marshal(0x10fad60, 0xc4200763e0, 0xc420041ea8, 0x1, 0x1, 0x53, 0x0)
/Users/sreekanth/workspace_fix_json/godeps/src/github.com/json-iterator/go/feature_adapter.go:43 +0x49
main.jsoniterMarshal(0x64, 0xc42007c4e0)
/Users/sreekanth/jsoniter/mapFails.go:12 +0x87
main.main()
/Users/sreekanth/jsoniter/mapFails.go:66 +0x296
exit status 2

@taowen taowen reopened this Dec 7, 2017
@taowen taowen closed this as completed in d2a7335 Dec 8, 2017
zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants