Skip to content

Commit

Permalink
fix: check EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 3, 2023
1 parent 680ace8 commit b724d71
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark-linux-arm64.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Benchmark Linux-X64
name: Benchmark Linux-ARM

on: pull_request

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push-check-qemu.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Push Check Qemu-ARM
name: Push Check Linux-Qemu

on: push

Expand Down
4 changes: 1 addition & 3 deletions ast/api_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package ast
import (
`encoding/base64`
`encoding/json`
_ `unsafe`

`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
Expand All @@ -25,8 +24,7 @@ func unquote(src string) (string, types.ParsingError) {
return rt.Mem2Str(out), 0
}

//go:linkname unquoteBytes encoding/json.unquoteBytes
func unquoteBytes(s []byte) (t []byte, ok bool)


func decodeBase64(src string) ([]byte, error) {
return base64.StdEncoding.DecodeString(src)
Expand Down
23 changes: 15 additions & 8 deletions ast/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func decodeString(src string, pos int) (ret int, v string) {
return ret, v
}

vv, err := strconv.Unquote(src[pos:ret])
if err != nil {
vv, ok := unquoteBytes(rt.Str2Mem(src[pos:ret]))
if !ok {
return -int(types.ERR_INVALID_CHAR), ""
}

runtime.KeepAlive(src)
return ret, vv
return ret, rt.Mem2Str(vv)
}

func decodeBinary(src string, pos int) (ret int, v []byte) {
Expand Down Expand Up @@ -120,7 +120,7 @@ func decodeInt64(src string, pos int) (ret int, v int64, err error) {
return -int(types.ERR_EOF), 0, nil
}

if c := *(*byte)(unsafe.Pointer(sp)); c == '-' || c == '+' {
if c := *(*byte)(unsafe.Pointer(sp)); c == '-' {
sp += 1
}
if sp == se {
Expand All @@ -133,8 +133,10 @@ func decodeInt64(src string, pos int) (ret int, v int64, err error) {
}
}

if c := *(*byte)(unsafe.Pointer(sp)); c == '.' || c == 'e' || c == 'E' {
return -int(types.ERR_INVALID_NUMBER_FMT), 0, nil
if sp < se {
if c := *(*byte)(unsafe.Pointer(sp)); c == '.' || c == 'e' || c == 'E' {
return -int(types.ERR_INVALID_NUMBER_FMT), 0, nil
}
}

var vv string
Expand Down Expand Up @@ -167,7 +169,7 @@ func decodeFloat64(src string, pos int) (ret int, v float64, err error) {
return -int(types.ERR_EOF), 0, nil
}

if c := *(*byte)(unsafe.Pointer(sp)); c == '-' || c == '+' {
if c := *(*byte)(unsafe.Pointer(sp)); c == '-' {
sp += 1
}
if sp == se {
Expand Down Expand Up @@ -260,7 +262,7 @@ func skipNumber(src string, pos int) (ret int) {
return -int(types.ERR_EOF)
}

if c := *(*byte)(unsafe.Pointer(sp)); c == '-' || c == '+' {
if c := *(*byte)(unsafe.Pointer(sp)); c == '-' {
sp += 1
}
ss := sp
Expand Down Expand Up @@ -345,6 +347,11 @@ func skipString(src string, pos int) (ret int, ep int) {
break
}
}

if sp > se {
return -int(types.ERR_EOF), -1
}

runtime.KeepAlive(src)
return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)), ep
}
Expand Down
9 changes: 0 additions & 9 deletions ast/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package ast

import (
_ `unsafe`
`sync`
`unicode/utf8`
)
Expand All @@ -26,14 +25,6 @@ const (
_MaxBuffer = 1024 // 1KB buffer size
)

var (
//go:linkname safeSet encoding/json.safeSet
safeSet [utf8.RuneSelf]bool

//go:linkname hex encoding/json.hex
hex string
)

func quoteString(e *[]byte, s string) {
*e = append(*e, '"')
start := 0
Expand Down
15 changes: 14 additions & 1 deletion ast/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ast
import (
`unsafe`
`reflect`
`unicode/utf8`

`github.com/bytedance/sonic/internal/rt`
)
Expand Down Expand Up @@ -65,4 +66,16 @@ func addr2str(p unsafe.Pointer, n int64) (s string) {
(*rt.GoString)(unsafe.Pointer(&s)).Ptr = p
(*rt.GoString)(unsafe.Pointer(&s)).Len = int(n)
return
}
}


var (
//go:linkname safeSet encoding/json.safeSet
safeSet [utf8.RuneSelf]bool

//go:linkname hex encoding/json.hex
hex string
)

//go:linkname unquoteBytes encoding/json.unquoteBytes
func unquoteBytes(s []byte) (t []byte, ok bool)

0 comments on commit b724d71

Please sign in to comment.