Skip to content

Commit 7b3f9c6

Browse files
committed
Fix to throw an overflow error before an illegal number error due to unexpected e, E, or . byte when reading Byte, Short, Int, Long, and BigInt values
1 parent 301ed10 commit 7b3f9c6

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,8 @@ final class JsonReader private[jsoniter_scala](
20602060
head = pos
20612061
x ^= s
20622062
x -= s
2063-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20642063
if (x == 128) byteOverflowError(pos - 1)
2064+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20652065
}
20662066
x.toByte
20672067
}
@@ -2096,8 +2096,8 @@ final class JsonReader private[jsoniter_scala](
20962096
head = pos
20972097
x ^= s
20982098
x -= s
2099-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21002099
if (x == 32768) shortOverflowError(pos - 1)
2100+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21012101
}
21022102
x.toShort
21032103
}
@@ -2134,8 +2134,8 @@ final class JsonReader private[jsoniter_scala](
21342134
head = pos
21352135
x ^= s
21362136
x -= s
2137-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21382137
if ((s & x) == -2147483648) intOverflowError(pos - 1)
2138+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21392139
}
21402140
x
21412141
}
@@ -2184,11 +2184,11 @@ final class JsonReader private[jsoniter_scala](
21842184
pos += 1
21852185
}
21862186
head = pos
2187-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21882187
if (isPos) {
21892188
if (x == -9223372036854775808L) longOverflowError(pos - 1)
21902189
x = -x
21912190
}
2191+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21922192
x
21932193
}
21942194
}
@@ -2541,10 +2541,10 @@ final class JsonReader private[jsoniter_scala](
25412541
b >= '0' && b <= '9'
25422542
}) pos += 1
25432543
head = pos
2544-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
25452544
if (mark == 0) from -= newMark
25462545
if (mark > oldMark) mark = oldMark
25472546
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1)
2547+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
25482548
toBigInt(buf, from, pos, s)
25492549
}
25502550
}

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -2022,8 +2022,8 @@ final class JsonReader private[jsoniter_scala](
20222022
head = pos
20232023
x ^= s
20242024
x -= s
2025-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20262025
if (x == 128) byteOverflowError(pos - 1)
2026+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20272027
}
20282028
x.toByte
20292029
}
@@ -2058,8 +2058,8 @@ final class JsonReader private[jsoniter_scala](
20582058
head = pos
20592059
x ^= s
20602060
x -= s
2061-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20622061
if (x == 32768) shortOverflowError(pos - 1)
2062+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20632063
}
20642064
x.toShort
20652065
}
@@ -2096,8 +2096,8 @@ final class JsonReader private[jsoniter_scala](
20962096
head = pos
20972097
x ^= s
20982098
x -= s
2099-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21002099
if ((s & x) == -2147483648) intOverflowError(pos - 1)
2100+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21012101
}
21022102
x
21032103
}
@@ -2152,8 +2152,8 @@ final class JsonReader private[jsoniter_scala](
21522152
head = pos
21532153
x ^= s
21542154
x -= s
2155-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21562155
if ((s & x) == -9223372036854775808L) longOverflowError(pos - 1)
2156+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21572157
}
21582158
x
21592159
}
@@ -2513,10 +2513,10 @@ final class JsonReader private[jsoniter_scala](
25132513
b = (bs >> (offset << 3)).toByte
25142514
}
25152515
head = pos
2516-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
25172516
if (mark == 0) from -= newMark
25182517
if (mark > oldMark) mark = oldMark
25192518
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1)
2519+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
25202520
toBigInt(buf, from, pos, s)
25212521
}
25222522
}

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -2022,8 +2022,8 @@ final class JsonReader private[jsoniter_scala](
20222022
head = pos
20232023
x ^= s
20242024
x -= s
2025-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20262025
if (x == 128) byteOverflowError(pos - 1)
2026+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20272027
}
20282028
x.toByte
20292029
}
@@ -2058,8 +2058,8 @@ final class JsonReader private[jsoniter_scala](
20582058
head = pos
20592059
x ^= s
20602060
x -= s
2061-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20622061
if (x == 32768) shortOverflowError(pos - 1)
2062+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
20632063
}
20642064
x.toShort
20652065
}
@@ -2096,8 +2096,8 @@ final class JsonReader private[jsoniter_scala](
20962096
head = pos
20972097
x ^= s
20982098
x -= s
2099-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21002099
if ((s & x) == -2147483648) intOverflowError(pos - 1)
2100+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21012101
}
21022102
x
21032103
}
@@ -2152,8 +2152,8 @@ final class JsonReader private[jsoniter_scala](
21522152
head = pos
21532153
x ^= s
21542154
x -= s
2155-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21562155
if ((s & x) == -9223372036854775808L) longOverflowError(pos - 1)
2156+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
21572157
}
21582158
x
21592159
}
@@ -2510,10 +2510,10 @@ final class JsonReader private[jsoniter_scala](
25102510
b = (bs >> (offset << 3)).toByte
25112511
}
25122512
head = pos
2513-
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
25142513
if (mark == 0) from -= newMark
25152514
if (mark > oldMark) mark = oldMark
25162515
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1)
2516+
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
25172517
toBigInt(buf, from, pos, s)
25182518
}
25192519
}

jsoniter-scala-core/shared/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReaderSpec.scala

+33
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
24182418
"throw parsing exception on byte overflow" in {
24192419
checkError("128", "value is too large for byte, offset: 0x00000002",
24202420
"value is too large for byte, offset: 0x00000003")
2421+
checkError("128.", "value is too large for byte, offset: 0x00000002",
2422+
"value is too large for byte, offset: 0x00000003")
2423+
checkError("128e", "value is too large for byte, offset: 0x00000002",
2424+
"value is too large for byte, offset: 0x00000003")
2425+
checkError("128E", "value is too large for byte, offset: 0x00000002",
2426+
"value is too large for byte, offset: 0x00000003")
24212427
checkError("-129", "value is too large for byte, offset: 0x00000003",
24222428
"value is too large for byte, offset: 0x00000004")
24232429
checkError("12345", "value is too large for byte, offset: 0x00000003",
@@ -2467,6 +2473,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
24672473
"throw parsing exception on short overflow" in {
24682474
checkError("32768", "value is too large for short, offset: 0x00000004",
24692475
"value is too large for short, offset: 0x00000005")
2476+
checkError("32768.", "value is too large for short, offset: 0x00000004",
2477+
"value is too large for short, offset: 0x00000005")
2478+
checkError("32768e", "value is too large for short, offset: 0x00000004",
2479+
"value is too large for short, offset: 0x00000005")
2480+
checkError("32768E", "value is too large for short, offset: 0x00000004",
2481+
"value is too large for short, offset: 0x00000005")
24702482
checkError("-32769", "value is too large for short, offset: 0x00000005",
24712483
"value is too large for short, offset: 0x00000006")
24722484
checkError("12345678901", "value is too large for short, offset: 0x00000005",
@@ -2516,6 +2528,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
25162528
"throw parsing exception on int overflow" in {
25172529
checkError("2147483648", "value is too large for int, offset: 0x00000009",
25182530
"value is too large for int, offset: 0x0000000a")
2531+
checkError("2147483648.", "value is too large for int, offset: 0x00000009",
2532+
"value is too large for int, offset: 0x0000000a")
2533+
checkError("2147483648e", "value is too large for int, offset: 0x00000009",
2534+
"value is too large for int, offset: 0x0000000a")
2535+
checkError("2147483648E", "value is too large for int, offset: 0x00000009",
2536+
"value is too large for int, offset: 0x0000000a")
25192537
checkError("-2147483649", "value is too large for int, offset: 0x0000000a",
25202538
"value is too large for int, offset: 0x0000000b")
25212539
checkError("12345678901", "value is too large for int, offset: 0x0000000a",
@@ -2569,6 +2587,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
25692587
"throw parsing exception on long overflow" in {
25702588
checkError("9223372036854775808", "value is too large for long, offset: 0x00000012",
25712589
"value is too large for long, offset: 0x00000013")
2590+
checkError("9223372036854775808.", "value is too large for long, offset: 0x00000012",
2591+
"value is too large for long, offset: 0x00000013")
2592+
checkError("9223372036854775808e", "value is too large for long, offset: 0x00000012",
2593+
"value is too large for long, offset: 0x00000013")
2594+
checkError("9223372036854775808E", "value is too large for long, offset: 0x00000012",
2595+
"value is too large for long, offset: 0x00000013")
25722596
checkError("-9223372036854775809", "value is too large for long, offset: 0x00000013",
25732597
"value is too large for long, offset: 0x00000014")
25742598
checkError("12345678901234567890", "value is too large for long, offset: 0x00000013",
@@ -2879,6 +2903,15 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
28792903
checkError("9" * 308,
28802904
"value exceeds limit for number of digits, offset: 0x00000133",
28812905
"value exceeds limit for number of digits, offset: 0x00000134")
2906+
checkError("9" * 308 + ".",
2907+
"value exceeds limit for number of digits, offset: 0x00000133",
2908+
"value exceeds limit for number of digits, offset: 0x00000134")
2909+
checkError("9" * 308 + "e",
2910+
"value exceeds limit for number of digits, offset: 0x00000133",
2911+
"value exceeds limit for number of digits, offset: 0x00000134")
2912+
checkError("9" * 308 + "E",
2913+
"value exceeds limit for number of digits, offset: 0x00000133",
2914+
"value exceeds limit for number of digits, offset: 0x00000134")
28822915
checkError(s"-${"9" * 308}",
28832916
"value exceeds limit for number of digits, offset: 0x00000134",
28842917
"value exceeds limit for number of digits, offset: 0x00000135")

0 commit comments

Comments
 (0)