Skip to content

Commit

Permalink
Fix parsing of large integer doubles (#504)
Browse files Browse the repository at this point in the history
Fixes #240

The basic problem appears to be that we are using a `parseLong`
fast-path inside `ujson.Value.visitFloat64StringParts`. This is only
valid for numbers of a certain digit-length. We should be checking for
numbers larger than that length, and falling back to the `toDouble` slow
path for those
  • Loading branch information
lihaoyi authored Jul 9, 2023
1 parent 8723cae commit 1e75232
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ujson/src/ujson/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ object Value extends AstTransformer[Value]{

override def visitFloat64StringParts(s: CharSequence, decIndex: Int, expIndex: Int, index: Int) = {
ujson.Num(
if (decIndex != -1 || expIndex != -1) s.toString.toDouble
if (decIndex != -1 || expIndex != -1 || s.length() >= 19 /*digit count of largest positive long*/) {
s.toString.toDouble
}
else ParseUtils.parseIntegralNum(s, decIndex, expIndex, index).toDouble
)
}
Expand Down
44 changes: 44 additions & 0 deletions ujson/test/src/ujson/SmallJsonTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,50 @@ object SmallJsonTests extends TestSuite {

}
}
test("largeDouble"){
test("longMaxValue") {
assert(
ujson.read("9223372036854775807").num ==
"9223372036854775807".toDouble
)
}
test("longMinValue") {
assert(
ujson.read("-9223372036854775808").num ==
"-9223372036854775808".toDouble
)
}
test("longMaxValuePlusOne") {
assert(
ujson.read("9223372036854775808").num ==
"9223372036854775808".toDouble
)
}
test("longMinValueMinusOne") {
assert(
ujson.read("-9223372036854775809").num ==
"-9223372036854775809".toDouble
)
}
test("issue240"){
assert(
ujson.read("28291041994989161181883157038606177750").num ==
"28291041994989161181883157038606177750".toDouble
)
}
test("largeDecimalPoint"){
assert(
ujson.read("28291041994989161181883157038606177750.28291041994989161181883157038606177750").num ==
"28291041994989161181883157038606177750.28291041994989161181883157038606177750".toDouble
)
}
test("largeDecimalPointExponent"){
assert(
ujson.read("28291041994989161181883157038606177750.28291041994989161181883157038606177750E123456789").num ==
"28291041994989161181883157038606177750.28291041994989161181883157038606177750E123456789".toDouble
)
}
}
test("inputsEu"){
TestUtil.checkParse(
"""{
Expand Down
5 changes: 4 additions & 1 deletion upickle/core/templates/ElemUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ object ElemUtils{
}

val size = end - i
if (size <= 0 || size > 19) throw new NumberFormatException(new String(cs))

if (size <= 0 || size > 19 /*digit count of largest positive long*/) {
throw new NumberFormatException(new String(cs))
}

while (i < end) {
val digit = cs(i).toInt - 48
Expand Down

0 comments on commit 1e75232

Please sign in to comment.