2017-11-20 09:30:52 -05:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2018-07-25 04:17:02 -04:00
|
|
|
"encoding/json"
|
2017-11-20 09:30:52 -05:00
|
|
|
"io"
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var floatDigits []int8
|
|
|
|
|
|
|
|
const invalidCharForNumber = int8(-1)
|
|
|
|
const endOfNumber = int8(-2)
|
|
|
|
const dotInNumber = int8(-3)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
floatDigits = make([]int8, 256)
|
|
|
|
for i := 0; i < len(floatDigits); i++ {
|
|
|
|
floatDigits[i] = invalidCharForNumber
|
|
|
|
}
|
|
|
|
for i := int8('0'); i <= int8('9'); i++ {
|
|
|
|
floatDigits[i] = i - int8('0')
|
|
|
|
}
|
|
|
|
floatDigits[','] = endOfNumber
|
|
|
|
floatDigits[']'] = endOfNumber
|
|
|
|
floatDigits['}'] = endOfNumber
|
|
|
|
floatDigits[' '] = endOfNumber
|
|
|
|
floatDigits['\t'] = endOfNumber
|
|
|
|
floatDigits['\n'] = endOfNumber
|
|
|
|
floatDigits['.'] = dotInNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadBigFloat read big.Float
|
|
|
|
func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
|
|
|
|
str := iter.readNumberAsString()
|
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
prec := 64
|
|
|
|
if len(str) > prec {
|
|
|
|
prec = len(str)
|
|
|
|
}
|
|
|
|
val, _, err := big.ParseFloat(str, 10, uint(prec), big.ToZero)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadBigInt read big.Int
|
|
|
|
func (iter *Iterator) ReadBigInt() (ret *big.Int) {
|
|
|
|
str := iter.readNumberAsString()
|
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ret = big.NewInt(0)
|
|
|
|
var success bool
|
|
|
|
ret, success = ret.SetString(str, 10)
|
|
|
|
if !success {
|
|
|
|
iter.ReportError("ReadBigInt", "invalid big int")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
//ReadFloat32 read float32
|
|
|
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
|
|
|
c := iter.nextToken()
|
|
|
|
if c == '-' {
|
|
|
|
return -iter.readPositiveFloat32()
|
|
|
|
}
|
|
|
|
iter.unreadByte()
|
|
|
|
return iter.readPositiveFloat32()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
|
|
|
i := iter.head
|
|
|
|
// first char
|
|
|
|
if i == iter.tail {
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
2019-03-19 11:30:11 -04:00
|
|
|
c := iter.buf[i]
|
2017-11-20 09:30:52 -05:00
|
|
|
i++
|
|
|
|
ind := floatDigits[c]
|
|
|
|
switch ind {
|
|
|
|
case invalidCharForNumber:
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
case endOfNumber:
|
|
|
|
iter.ReportError("readFloat32", "empty number")
|
|
|
|
return
|
|
|
|
case dotInNumber:
|
|
|
|
iter.ReportError("readFloat32", "leading dot is invalid")
|
|
|
|
return
|
|
|
|
case 0:
|
|
|
|
if i == iter.tail {
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
|
|
|
c = iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
iter.ReportError("readFloat32", "leading zero is invalid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 11:30:11 -04:00
|
|
|
value := uint64(ind)
|
2017-11-20 09:30:52 -05:00
|
|
|
// chars before dot
|
|
|
|
non_decimal_loop:
|
|
|
|
for ; i < iter.tail; i++ {
|
|
|
|
c = iter.buf[i]
|
|
|
|
ind := floatDigits[c]
|
|
|
|
switch ind {
|
|
|
|
case invalidCharForNumber:
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
case endOfNumber:
|
|
|
|
iter.head = i
|
|
|
|
return float32(value)
|
|
|
|
case dotInNumber:
|
|
|
|
break non_decimal_loop
|
|
|
|
}
|
|
|
|
if value > uint64SafeToMultiple10 {
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
|
|
|
}
|
|
|
|
// chars after dot
|
|
|
|
if c == '.' {
|
|
|
|
i++
|
|
|
|
decimalPlaces := 0
|
|
|
|
if i == iter.tail {
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
|
|
|
for ; i < iter.tail; i++ {
|
|
|
|
c = iter.buf[i]
|
|
|
|
ind := floatDigits[c]
|
|
|
|
switch ind {
|
|
|
|
case endOfNumber:
|
|
|
|
if decimalPlaces > 0 && decimalPlaces < len(pow10) {
|
|
|
|
iter.head = i
|
|
|
|
return float32(float64(value) / float64(pow10[decimalPlaces]))
|
|
|
|
}
|
|
|
|
// too many decimal places
|
|
|
|
return iter.readFloat32SlowPath()
|
2019-03-19 11:30:11 -04:00
|
|
|
case invalidCharForNumber, dotInNumber:
|
2017-11-20 09:30:52 -05:00
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
|
|
|
decimalPlaces++
|
|
|
|
if value > uint64SafeToMultiple10 {
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readNumberAsString() (ret string) {
|
|
|
|
strBuf := [16]byte{}
|
|
|
|
str := strBuf[0:0]
|
|
|
|
load_loop:
|
|
|
|
for {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
str = append(str, c)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
iter.head = i
|
|
|
|
break load_loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !iter.loadMore() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(str) == 0 {
|
|
|
|
iter.ReportError("readNumberAsString", "invalid number")
|
|
|
|
}
|
|
|
|
return *(*string)(unsafe.Pointer(&str))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
|
|
|
str := iter.readNumberAsString()
|
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errMsg := validateFloat(str)
|
|
|
|
if errMsg != "" {
|
|
|
|
iter.ReportError("readFloat32SlowPath", errMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val, err := strconv.ParseFloat(str, 32)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return float32(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFloat64 read float64
|
|
|
|
func (iter *Iterator) ReadFloat64() (ret float64) {
|
|
|
|
c := iter.nextToken()
|
|
|
|
if c == '-' {
|
|
|
|
return -iter.readPositiveFloat64()
|
|
|
|
}
|
|
|
|
iter.unreadByte()
|
|
|
|
return iter.readPositiveFloat64()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
|
|
|
i := iter.head
|
|
|
|
// first char
|
|
|
|
if i == iter.tail {
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
2019-03-19 11:30:11 -04:00
|
|
|
c := iter.buf[i]
|
2017-11-20 09:30:52 -05:00
|
|
|
i++
|
|
|
|
ind := floatDigits[c]
|
|
|
|
switch ind {
|
|
|
|
case invalidCharForNumber:
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
case endOfNumber:
|
|
|
|
iter.ReportError("readFloat64", "empty number")
|
|
|
|
return
|
|
|
|
case dotInNumber:
|
|
|
|
iter.ReportError("readFloat64", "leading dot is invalid")
|
|
|
|
return
|
|
|
|
case 0:
|
|
|
|
if i == iter.tail {
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
|
|
|
c = iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
iter.ReportError("readFloat64", "leading zero is invalid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 11:30:11 -04:00
|
|
|
value := uint64(ind)
|
2017-11-20 09:30:52 -05:00
|
|
|
// chars before dot
|
|
|
|
non_decimal_loop:
|
|
|
|
for ; i < iter.tail; i++ {
|
|
|
|
c = iter.buf[i]
|
|
|
|
ind := floatDigits[c]
|
|
|
|
switch ind {
|
|
|
|
case invalidCharForNumber:
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
case endOfNumber:
|
|
|
|
iter.head = i
|
|
|
|
return float64(value)
|
|
|
|
case dotInNumber:
|
|
|
|
break non_decimal_loop
|
|
|
|
}
|
|
|
|
if value > uint64SafeToMultiple10 {
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
|
|
|
|
}
|
|
|
|
// chars after dot
|
|
|
|
if c == '.' {
|
|
|
|
i++
|
|
|
|
decimalPlaces := 0
|
|
|
|
if i == iter.tail {
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
|
|
|
for ; i < iter.tail; i++ {
|
|
|
|
c = iter.buf[i]
|
|
|
|
ind := floatDigits[c]
|
|
|
|
switch ind {
|
|
|
|
case endOfNumber:
|
|
|
|
if decimalPlaces > 0 && decimalPlaces < len(pow10) {
|
|
|
|
iter.head = i
|
|
|
|
return float64(value) / float64(pow10[decimalPlaces])
|
|
|
|
}
|
|
|
|
// too many decimal places
|
|
|
|
return iter.readFloat64SlowPath()
|
2019-03-19 11:30:11 -04:00
|
|
|
case invalidCharForNumber, dotInNumber:
|
2017-11-20 09:30:52 -05:00
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
|
|
|
decimalPlaces++
|
|
|
|
if value > uint64SafeToMultiple10 {
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind)
|
[20.10] vendor: github.com/json-iterator/go v1.1.12 for Go 1.18 compatibility
full diff: https://github.com/json-iterator/go/compare/0ff49de124c6f76f8494e194af75bde0f1a49a29...024077e996b048517130b21ea6bf12aa23055d3d
Fixes a nil-pointer exception on go 1.18;
```
=== FAIL: cli/context/kubernetes TestSaveLoadContexts (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x40fcbc]
goroutine 19 [running]:
testing.tRunner.func1.2({0xa7e080, 0x1073930})
/usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0xa7e080, 0x1073930})
/usr/local/go/src/runtime/panic.go:838 +0x207
reflect.mapiternext(0x40?)
/usr/local/go/src/runtime/map.go:1378 +0x19
github.com/docker/cli/vendor/github.com/modern-go/reflect2.(*UnsafeMapIterator).UnsafeNext(0x8?)
/go/src/github.com/docker/cli/vendor/github.com/modern-go/reflect2/unsafe_map.go:136 +0x32
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sortKeysMapEncoder).Encode(0xc000478930, 0xc0000ca3a8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_map.go:293 +0x335
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046c898?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc000482630, 0xa2790c?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482780, 0xb3a599?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*placeholderEncoder).Encode(0xc00046ca10?, 0x95d787?, 0xc0000bae58?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:327 +0x22
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004829f0, 0xa0fd11?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000482a50, 0x40aa15?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*sliceEncoder).Encode(0xc00047e198, 0xc0003a83c8, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_slice.go:38 +0x2bb
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structFieldEncoder).Encode(0xc0004837a0, 0xa12e12?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:110 +0x56
github.com/docker/cli/vendor/github.com/json-iterator/go.(*structEncoder).Encode(0xc000483890, 0x0?, 0xc0000bae40)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_struct_encoder.go:158 +0x652
github.com/docker/cli/vendor/github.com/json-iterator/go.(*OptionalEncoder).Encode(0xc0003b6be0?, 0x0?, 0x0?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect_optional.go:74 +0xa4
github.com/docker/cli/vendor/github.com/json-iterator/go.(*onePtrEncoder).Encode(0xc000471e30, 0xc0003a8370, 0xc000460720?)
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:214 +0x82
github.com/docker/cli/vendor/github.com/json-iterator/go.(*Stream).WriteVal(0xc0000bae40, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/reflect.go:93 +0x158
github.com/docker/cli/vendor/github.com/json-iterator/go.(*frozenConfig).Marshal(0xc0003b6be0, {0xabe4a0, 0xc0003a8370})
/go/src/github.com/docker/cli/vendor/github.com/json-iterator/go/config.go:299 +0xc9
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json.(*Serializer).Encode(0xc00043aee0?, {0xc375c0?, 0xc0003a8370?}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go:310 +0x6d
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning.(*codec).Encode(0xc0000f8480, {0xc37570?, 0xc0000bacc0}, {0xc339e0, 0xc000460210})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go:231 +0x926
github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime.Encode({0x7f48b36ce5c0, 0xc0000f8480}, {0xc37570, 0xc0000bacc0})
/go/src/github.com/docker/cli/vendor/k8s.io/apimachinery/pkg/runtime/codec.go:46 +0x64
github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd.Write(...)
/go/src/github.com/docker/cli/vendor/k8s.io/client-go/tools/clientcmd/loader.go:469
github.com/docker/cli/cli/context/kubernetes.TestSaveLoadContexts(0xc0004561a0?)
/go/src/github.com/docker/cli/cli/context/kubernetes/endpoint_test.go:75 +0xf0a
testing.tRunner(0xc0004561a0, 0xb89ea0)
/usr/local/go/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1486 +0x35f
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-08-19 10:12:25 -04:00
|
|
|
if value > maxFloat64 {
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return iter.readFloat64SlowPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
|
|
|
str := iter.readNumberAsString()
|
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errMsg := validateFloat(str)
|
|
|
|
if errMsg != "" {
|
|
|
|
iter.ReportError("readFloat64SlowPath", errMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val, err := strconv.ParseFloat(str, 64)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFloat(str string) string {
|
|
|
|
// strconv.ParseFloat is not validating `1.` or `1.e1`
|
|
|
|
if len(str) == 0 {
|
|
|
|
return "empty number"
|
|
|
|
}
|
|
|
|
if str[0] == '-' {
|
|
|
|
return "-- is not valid"
|
|
|
|
}
|
|
|
|
dotPos := strings.IndexByte(str, '.')
|
|
|
|
if dotPos != -1 {
|
|
|
|
if dotPos == len(str)-1 {
|
|
|
|
return "dot can not be last character"
|
|
|
|
}
|
|
|
|
switch str[dotPos+1] {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
default:
|
|
|
|
return "missing digit after dot"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2018-07-25 04:17:02 -04:00
|
|
|
|
|
|
|
// ReadNumber read json.Number
|
|
|
|
func (iter *Iterator) ReadNumber() (ret json.Number) {
|
|
|
|
return json.Number(iter.readNumberAsString())
|
|
|
|
}
|