ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.85k stars 295 forks source link

1.1.7 encode crash #313

Closed shy3533 closed 4 years ago

shy3533 commented 5 years ago

{"code":0, "data":{"text":"�ɪV�+|�!��^r�Ǘ�\u0026\b\u0005˵U۳.\f���","texts":[{"source":"v1Vg24nSA19fK0b67rX1YGToxXbv8mz52VNfFus3TDU7A=","target":"�ɪV�+|�!��^r�Ǘ�\u0026\b\u0005˵U۳.\f���"}]}}

when i decode this string to map[string]interface{}, it work fine. but encode to json string, the encoder use all of my memory, the app crashed

ugorji commented 5 years ago

I don't understand what you are saying.

Can you please attach a reproducer that I can run with "go run" or "go test". This should clearly say what is expected to happen, and I run it and see what happens.

shy3533 commented 5 years ago

package main

import ( "fmt" "reflect"

"github.com/ugorji/go/codec"

)

func main(){ jsonData:={"code":0, "data":{"text":"�ɪV�+|�!��^r�Ǘ�\u0026\b\u0005˵U۳.\f���","texts":[{"source":"v1Vg24nSA19fK0b67rX1YGToxXbv8mz52VNfFus3TDU7A=","target":"�ɪV�+|�!��^r�Ǘ�\u0026\b\u0005˵U۳.\f���"}]}}

data:=map[string]interface{}{} err:=NewGJsonCodec().Unmarshal([]byte(jsonData),&data) fmt.Print(err,data) derr,d:=NewGJsonCodec().Marshal(data) // crash fmt.Print(derr,d)

}

type GJsonCodec struct { jh codec.JsonHandle }

func NewGJsonCodec() GJsonCodec { c := &GJsonCodec{} c.jh.DecodeOptions.ReaderBufferSize = 16 1024 1024 c.jh.EncodeOptions.WriterBufferSize = 16 1024 * 1024 c.jh.DecodeOptions.SignedInteger = true c.jh.DecodeOptions.MapType = reflect.TypeOf(map[string]interface{}(nil)) return c }

func (j *GJsonCodec) Marshal(v interface{}) ([]byte, error) { switch v.(type) { case []byte: { return v.([]byte), nil } default: { var raw []byte enc := codec.NewEncoderBytes(&raw, &j.jh) err := enc.Encode(v) return raw, err } } }

func (j GJsonCodec) Unmarshal(data []byte, v interface{}) error { switch v.(type) { case []byte: { out := v.([]byte) if out != nil { out = append(*out, data...) } return nil } default: { dec := codec.NewDecoderBytes(data, &j.jh) err := dec.Decode(&v) return err } } }

func (j *GJsonCodec) Name() string { return "gjson" }

func (j *GJsonCodec) String() string { return "gjson" }

cvermilion commented 5 years ago

I believe this is the same issue as #317 -- the problem is the Unicode replacement character \ufffd.