Add validation for decode_float

This commit is contained in:
Masaaki Goshima 2020-08-25 00:56:01 +09:00
parent 84486c012b
commit d2bc6b002c
1 changed files with 37 additions and 16 deletions

View File

@ -13,7 +13,8 @@ func newFloatDecoder(op func(uintptr, float64)) *floatDecoder {
return &floatDecoder{op: op}
}
var floatTable = [256]bool{
var (
floatTable = [256]bool{
'0': true,
'1': true,
'2': true,
@ -28,8 +29,22 @@ var floatTable = [256]bool{
'e': true,
'E': true,
'+': true,
'-': true,
}
validEndNumberChar = [256]bool{
nul: true,
' ': true,
'\t': true,
'\r': true,
'\n': true,
',': true,
':': true,
'}': true,
']': true,
}
)
func floatBytes(s *stream) []byte {
start := s.cursor
for {
@ -97,6 +112,9 @@ func (d *floatDecoder) decodeStream(s *stream, p uintptr) error {
if err != nil {
return err
}
if !validEndNumberChar[s.char()] {
return errUnexpectedEndOfJSON("float", s.totalOffset())
}
str := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(str, 64)
if err != nil {
@ -112,6 +130,9 @@ func (d *floatDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error
return 0, err
}
cursor = c
if !validEndNumberChar[buf[cursor]] {
return 0, errUnexpectedEndOfJSON("float", cursor)
}
s := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(s, 64)
if err != nil {