Skip to content

Commit

Permalink
Fix parseutils.parseBiggestInt regression (#10348)
Browse files Browse the repository at this point in the history
  • Loading branch information
GULPF authored and Araq committed Jan 18, 2019
1 parent 3cc39c2 commit 86a91c1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/pure/parseutils.nim
Original file line number Diff line number Diff line change
@@ -350,6 +350,10 @@ proc captureBetween*(s: string, first: char, second = '\0', start = 0): string =
proc integerOutOfRangeError() {.noinline.} =
raise newException(ValueError, "Parsed integer outside of valid range")

# See #6752
when defined(js):
{.push overflowChecks: off.}

proc rawParseInt(s: string, b: var BiggestInt, start = 0): int =
var
sign: BiggestInt = -1
@@ -363,18 +367,21 @@ proc rawParseInt(s: string, b: var BiggestInt, start = 0): int =
b = 0
while i < s.len and s[i] in {'0'..'9'}:
let c = ord(s[i]) - ord('0')
if b >= (low(int) + c) div 10:
if b >= (low(BiggestInt) + c) div 10:
b = b * 10 - c
else:
integerOutOfRangeError()
inc(i)
while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored
if sign == -1 and b == low(int):
if sign == -1 and b == low(BiggestInt):
integerOutOfRangeError()
else:
b = b * sign
result = i - start

when defined(js):
{.pop.} # overflowChecks: off

proc parseBiggestInt*(s: string, number: var BiggestInt, start = 0): int {.
rtl, extern: "npuParseBiggestInt", noSideEffect, raises: [ValueError].} =
## Parses an integer starting at `start` and stores the value into `number`.
@@ -632,4 +639,8 @@ when isMainModule:
doAssert(parseSaturatedNatural("1_000_000", value) == 9)
doAssert value == 1_000_000

var i64Value: int64
discard parseBiggestInt("9223372036854775807", i64Value)
doAssert i64Value == 9223372036854775807

{.pop.}

0 comments on commit 86a91c1

Please sign in to comment.