Skip to content

Commit

Permalink
Fix to throw an overflow error before an illegal number error due to …
Browse files Browse the repository at this point in the history
…unexpected `e`, `E`, or `.` byte when reading `Byte`, `Short`, `Int`, `Long`, and `BigInt` values
  • Loading branch information
plokhotnyuk committed Feb 9, 2024
1 parent 301ed10 commit 7b3f9c6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2060,8 +2060,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (x == 128) byteOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x.toByte
}
Expand Down Expand Up @@ -2096,8 +2096,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (x == 32768) shortOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x.toShort
}
Expand Down Expand Up @@ -2134,8 +2134,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if ((s & x) == -2147483648) intOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x
}
Expand Down Expand Up @@ -2184,11 +2184,11 @@ final class JsonReader private[jsoniter_scala](
pos += 1
}
head = pos
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (isPos) {
if (x == -9223372036854775808L) longOverflowError(pos - 1)
x = -x
}
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
x
}
}
Expand Down Expand Up @@ -2541,10 +2541,10 @@ final class JsonReader private[jsoniter_scala](
b >= '0' && b <= '9'
}) pos += 1
head = pos
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (mark == 0) from -= newMark
if (mark > oldMark) mark = oldMark
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
toBigInt(buf, from, pos, s)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2022,8 +2022,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (x == 128) byteOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x.toByte
}
Expand Down Expand Up @@ -2058,8 +2058,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (x == 32768) shortOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x.toShort
}
Expand Down Expand Up @@ -2096,8 +2096,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if ((s & x) == -2147483648) intOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x
}
Expand Down Expand Up @@ -2152,8 +2152,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if ((s & x) == -9223372036854775808L) longOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x
}
Expand Down Expand Up @@ -2513,10 +2513,10 @@ final class JsonReader private[jsoniter_scala](
b = (bs >> (offset << 3)).toByte
}
head = pos
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (mark == 0) from -= newMark
if (mark > oldMark) mark = oldMark
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
toBigInt(buf, from, pos, s)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2022,8 +2022,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (x == 128) byteOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x.toByte
}
Expand Down Expand Up @@ -2058,8 +2058,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (x == 32768) shortOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x.toShort
}
Expand Down Expand Up @@ -2096,8 +2096,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if ((s & x) == -2147483648) intOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x
}
Expand Down Expand Up @@ -2152,8 +2152,8 @@ final class JsonReader private[jsoniter_scala](
head = pos
x ^= s
x -= s
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if ((s & x) == -9223372036854775808L) longOverflowError(pos - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
}
x
}
Expand Down Expand Up @@ -2510,10 +2510,10 @@ final class JsonReader private[jsoniter_scala](
b = (bs >> (offset << 3)).toByte
}
head = pos
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
if (mark == 0) from -= newMark
if (mark > oldMark) mark = oldMark
if (pos - from >= digitsLimit) digitsLimitError(from + digitsLimit - 1)
if ((b | 0x20) == 'e' || b == '.') numberError(pos)
toBigInt(buf, from, pos, s)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
"throw parsing exception on byte overflow" in {
checkError("128", "value is too large for byte, offset: 0x00000002",
"value is too large for byte, offset: 0x00000003")
checkError("128.", "value is too large for byte, offset: 0x00000002",
"value is too large for byte, offset: 0x00000003")
checkError("128e", "value is too large for byte, offset: 0x00000002",
"value is too large for byte, offset: 0x00000003")
checkError("128E", "value is too large for byte, offset: 0x00000002",
"value is too large for byte, offset: 0x00000003")
checkError("-129", "value is too large for byte, offset: 0x00000003",
"value is too large for byte, offset: 0x00000004")
checkError("12345", "value is too large for byte, offset: 0x00000003",
Expand Down Expand Up @@ -2467,6 +2473,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
"throw parsing exception on short overflow" in {
checkError("32768", "value is too large for short, offset: 0x00000004",
"value is too large for short, offset: 0x00000005")
checkError("32768.", "value is too large for short, offset: 0x00000004",
"value is too large for short, offset: 0x00000005")
checkError("32768e", "value is too large for short, offset: 0x00000004",
"value is too large for short, offset: 0x00000005")
checkError("32768E", "value is too large for short, offset: 0x00000004",
"value is too large for short, offset: 0x00000005")
checkError("-32769", "value is too large for short, offset: 0x00000005",
"value is too large for short, offset: 0x00000006")
checkError("12345678901", "value is too large for short, offset: 0x00000005",
Expand Down Expand Up @@ -2516,6 +2528,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
"throw parsing exception on int overflow" in {
checkError("2147483648", "value is too large for int, offset: 0x00000009",
"value is too large for int, offset: 0x0000000a")
checkError("2147483648.", "value is too large for int, offset: 0x00000009",
"value is too large for int, offset: 0x0000000a")
checkError("2147483648e", "value is too large for int, offset: 0x00000009",
"value is too large for int, offset: 0x0000000a")
checkError("2147483648E", "value is too large for int, offset: 0x00000009",
"value is too large for int, offset: 0x0000000a")
checkError("-2147483649", "value is too large for int, offset: 0x0000000a",
"value is too large for int, offset: 0x0000000b")
checkError("12345678901", "value is too large for int, offset: 0x0000000a",
Expand Down Expand Up @@ -2569,6 +2587,12 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
"throw parsing exception on long overflow" in {
checkError("9223372036854775808", "value is too large for long, offset: 0x00000012",
"value is too large for long, offset: 0x00000013")
checkError("9223372036854775808.", "value is too large for long, offset: 0x00000012",
"value is too large for long, offset: 0x00000013")
checkError("9223372036854775808e", "value is too large for long, offset: 0x00000012",
"value is too large for long, offset: 0x00000013")
checkError("9223372036854775808E", "value is too large for long, offset: 0x00000012",
"value is too large for long, offset: 0x00000013")
checkError("-9223372036854775809", "value is too large for long, offset: 0x00000013",
"value is too large for long, offset: 0x00000014")
checkError("12345678901234567890", "value is too large for long, offset: 0x00000013",
Expand Down Expand Up @@ -2879,6 +2903,15 @@ class JsonReaderSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
checkError("9" * 308,
"value exceeds limit for number of digits, offset: 0x00000133",
"value exceeds limit for number of digits, offset: 0x00000134")
checkError("9" * 308 + ".",
"value exceeds limit for number of digits, offset: 0x00000133",
"value exceeds limit for number of digits, offset: 0x00000134")
checkError("9" * 308 + "e",
"value exceeds limit for number of digits, offset: 0x00000133",
"value exceeds limit for number of digits, offset: 0x00000134")
checkError("9" * 308 + "E",
"value exceeds limit for number of digits, offset: 0x00000133",
"value exceeds limit for number of digits, offset: 0x00000134")
checkError(s"-${"9" * 308}",
"value exceeds limit for number of digits, offset: 0x00000134",
"value exceeds limit for number of digits, offset: 0x00000135")
Expand Down

0 comments on commit 7b3f9c6

Please sign in to comment.