Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,41 @@ func parseObject(s string, c *cache, depth int) (*Value, string, error) {
}

func escapeString(dst []byte, s string) []byte {
if !hasSpecialChars(s) {
// Fast path - nothing to escape.
dst = append(dst, '"')
dst = append(dst, s...)
dst = append(dst, '"')
return dst
}

// Slow path.
return strconv.AppendQuote(dst, s)
}

func hasSpecialChars(s string) bool {
if strings.IndexByte(s, '"') >= 0 || strings.IndexByte(s, '\\') >= 0 {
return true
}
dst = append(dst, '"')
for i := 0; i < len(s); i++ {
if s[i] < 0x20 {
return true
}
}
return false
c := s[i]
switch {
case c == '"':
// quotation mark
dst = append(dst, []byte{'\\', '"'}...)
case c == '\\':
// reverse solidus
dst = append(dst, []byte{'\\', '\\'}...)
case c >= 0x20:
// default, rest below are control chars
dst = append(dst, c)
case c == 0x08:
dst = append(dst, []byte{'\\', 'b'}...)
case c < 0x09:
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', '0' + c}...)
case c == 0x09:
dst = append(dst, []byte{'\\', 't'}...)
case c == 0x0a:
dst = append(dst, []byte{'\\', 'n'}...)
case c == 0x0c:
dst = append(dst, []byte{'\\', 'f'}...)
case c == 0x0d:
dst = append(dst, []byte{'\\', 'r'}...)
case c < 0x10:
dst = append(dst, []byte{'\\', 'u', '0', '0', '0', 0x57 + c}...)
case c < 0x1a:
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x20 + c}...)
case c < 0x20:
dst = append(dst, []byte{'\\', 'u', '0', '0', '1', 0x47 + c}...)
}
}
dst = append(dst, '"')
return dst
}

func unescapeStringBestEffort(s string) string {
Expand Down
58 changes: 58 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fastjson

import (
"bytes"
"encoding/json"
"fmt"
"math"
"strings"
Expand Down Expand Up @@ -1275,3 +1277,59 @@ func testParseGetSerial(s string) error {
}
return nil
}

// Tests for https://github.com/valyala/fastjson/issues/90
// This was manifesting due to the use of strconv.AppendQuote
func TestUTF8NonPrintableArtifacts(t *testing.T) {
testCases := []struct {
name string
s string
}{
{
name: "problematic bytes",
s: "data:\"\\xd6`\\xb76d\\xf6E\U000E8737(\\x91\\xb294\"",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
jsonEnvelope := struct {
Inner string
}{
Inner: tc.s,
}

m, err := json.Marshal(jsonEnvelope) // `m` is a full valid json
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

// we will pass that `m` json through a fastjson marshal/unmarshal cycle

fastjsonValue, err := ParseBytes(m)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

o, err := fastjsonValue.Object()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

// In order to trigger the bug we need to visit all the keys and call Type() on them
o.Visit(func(k []byte, v *Value) {
v.Type()
})

res := fastjsonValue.MarshalTo(nil)
if !bytes.Equal(res, m) {
t.Fatalf("unexpected result; got %q; want %q", res, m)
}

err = ValidateBytes(res)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
})
}
}