Skip to content

Commit

Permalink
codec: json: leading zeros are not allowed in decimal numbers
Browse files Browse the repository at this point in the history
Fixes #408
  • Loading branch information
ugorji committed Mar 7, 2024
1 parent 2d571c5 commit 07c54c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 2 additions & 3 deletions codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4193,9 +4193,9 @@ func doTestJsonLargeInteger(t *testing.T, h Handle) {
var d *Decoder = NewDecoderBytes(nil, jh)
for _, v := range []tt{
{"0", true, true, 0, 0},
{"0000", true, true, 0, 0},
{"0000", false, false, 0, 0},
{"0.00e+2", true, true, 0, 0},
{"000e-2", true, true, 0, 0},
{"000e-2", false, false, 0, 0},
{"0.00e-2", true, true, 0, 0},

{"9223372036854775807", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
Expand Down Expand Up @@ -4384,7 +4384,6 @@ func doTestJsonNumberParsing(t *testing.T, h Handle) {
}
// try decoding it a uint64 or int64
fint, ffrac = math.Modf(f)
// xdebug2f(">> fint: %v (ffrac: %v) as uint64: %v as float64: %v", fint, ffrac, uint64(fint), float64(uint64(fint)))
bv = strconv.AppendFloat(bv[:0], fint, 'E', prec, 64)
if f < 0 || fint != float64(uint64(fint)) {
goto F64i
Expand Down
16 changes: 15 additions & 1 deletion codec/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func parseFloat64_custom(b []byte) (f float64, err error) {
}

func parseUint64_simple(b []byte) (n uint64, ok bool) {
if len(b) > 1 && b[0] == '0' { // punt on numbers with leading zeros
return
}

var i int
var n1 uint64
var c uint8
Expand Down Expand Up @@ -384,13 +388,23 @@ func readFloat(s []byte, y floatinfo) (r readFloatResult) {
i++
}

// we considered punting early if string has length > maxMantDigits, but this doesn't account
// considered punting early if string has length > maxMantDigits, but doesn't account
// for trailing 0's e.g. 700000000000000000000 can be encoded exactly as it is 7e20

var nd, ndMant, dp int8
var sawdot, sawexp bool
var xu uint64

if i+1 < slen && s[i] == '0' {
switch s[i+1] {
case '.', 'e', 'E':
// ok
default:
r.bad = true
return
}
}

LOOP:
for ; i < slen; i++ {
switch s[i] {
Expand Down

0 comments on commit 07c54c2

Please sign in to comment.