Skip to content

Commit

Permalink
parseint: basic implementation of overflow-detection [fixes #4874]
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Nov 23, 2013
1 parent 4aa6743 commit 36381b1
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1437,25 +1437,23 @@ function parseint{T<:Integer}(::Type{T}, s::String, base::Integer)
base = convert(T,base)
n::T = 0
while true
d = '0' <= c <= '9' ? c-'0' :
'A' <= c <= 'Z' ? c-'A'+10 :
'a' <= c <= 'z' ? c-'a'+10 : typemax(Int)
d::T = '0' <= c <= '9' ? c-'0' :
'A' <= c <= 'Z' ? c-'A'+10 :
'a' <= c <= 'z' ? c-'a'+10 : typemax(T)
if d >= base
isspace(c) || error("invalid digit $(repr(c)): $(repr(s))")
while !done(s,i)
c, i = next(s,i)
isspace(c) || error("extra characters after whitespace: $(repr(s))")
end
else
# TODO: overflow detection?
n = n*base + d
(T <: Signed) && (d *= sgn)
n = checked_mul(n,base)
n = checked_add(n,d)
end
done(s,i) && break
c, i = next(s,i)
end
if T <: Signed
n = flipsign(n,sgn)
end
return n
end

Expand Down

0 comments on commit 36381b1

Please sign in to comment.