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