Merge pull request #130 from orisano/fix/decode-escaped-string-to-empty-interface

fix: fails decode escaped string to empty interface
This commit is contained in:
Masaaki Goshima 2021-02-12 19:01:48 +09:00 committed by GitHub
commit 336ad146cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 16 deletions

View File

@ -275,23 +275,15 @@ func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsa
*(*interface{})(p) = v *(*interface{})(p) = v
}).decode(buf, cursor, p) }).decode(buf, cursor, p)
case '"': case '"':
cursor++ var v string
start := cursor ptr := unsafe.Pointer(&v)
for { dec := newStringDecoder(d.structName, d.fieldName)
switch buf[cursor] { cursor, err := dec.decode(buf, cursor, ptr)
case '\\': if err != nil {
cursor++ return 0, err
continue }
case '"': **(**interface{})(unsafe.Pointer(&p)) = v
literal := buf[start:cursor]
cursor++
**(**interface{})(unsafe.Pointer(&p)) = *(*string)(unsafe.Pointer(&literal))
return cursor, nil return cursor, nil
case nul:
return 0, errUnexpectedEndOfJSON("string", cursor)
}
cursor++
}
case 't': case 't':
if cursor+3 >= int64(len(buf)) { if cursor+3 >= int64(len(buf)) {
return 0, errUnexpectedEndOfJSON("bool(true)", cursor) return 0, errUnexpectedEndOfJSON("bool(true)", cursor)

View File

@ -197,6 +197,12 @@ func Test_Decoder(t *testing.T) {
assertEq(t, "interface.kind", "string", reflect.TypeOf(v).Kind().String()) assertEq(t, "interface.kind", "string", reflect.TypeOf(v).Kind().String())
assertEq(t, "interface", `hello`, fmt.Sprint(v)) assertEq(t, "interface", `hello`, fmt.Sprint(v))
}) })
t.Run("escaped string", func(t *testing.T) {
var v interface{}
assertErr(t, json.Unmarshal([]byte(`"he\"llo"`), &v))
assertEq(t, "interface.kind", "string", reflect.TypeOf(v).Kind().String())
assertEq(t, "interface", `he"llo`, fmt.Sprint(v))
})
t.Run("bool", func(t *testing.T) { t.Run("bool", func(t *testing.T) {
var v interface{} var v interface{}
assertErr(t, json.Unmarshal([]byte(`true`), &v)) assertErr(t, json.Unmarshal([]byte(`true`), &v))