Skip to content

Commit

Permalink
ndigits: check for invalid bases (fix JuliaLang#16766)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed May 3, 2017
1 parent 970a742 commit b7a9d7d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), xor,
binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod,
ndigits, promote_rule, rem, show, isqrt, string, powermod,
sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal,
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z, widen, signed, unsafe_trunc, trunc,
iszero
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0z,
ndigits0znb, widen, signed, unsafe_trunc, trunc, iszero

if Clong == Int32
const ClongMax = Union{Int8, Int16, Int32}
Expand Down Expand Up @@ -556,6 +556,7 @@ end

function ndigits0z(x::BigInt, b::Integer=10)
b < 2 && throw(DomainError())
x.size == 0 && return 0 # for consistency with other ndigits0z methods
if ispow2(b) && 2 <= b <= 62 # GMP assumes b is in this range
Int(ccall((:__gmpz_sizeinbase,:libgmp), Csize_t, (Ptr{BigInt}, Cint), &x, b))
else
Expand All @@ -575,7 +576,6 @@ function ndigits0z(x::BigInt, b::Integer=10)
end
end
end
ndigits(x::BigInt, b::Integer=10) = x.size == 0 ? 1 : ndigits0z(x,b)

prevpow2(x::BigInt) = x.size < 0 ? -prevpow2(-x) : (x <= 2 ? x : one(BigInt) << (ndigits(x, 2)-1))
nextpow2(x::BigInt) = x.size < 0 ? -nextpow2(-x) : (x <= 2 ? x : one(BigInt) << ndigits(x-1, 2))
Expand Down
28 changes: 18 additions & 10 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,11 @@ function ndigits0z(x::UInt128)
return n + ndigits0z(UInt64(x))
end
ndigits0z(x::Integer) = ndigits0z(unsigned(abs(x)))
ndigits(x::Integer) = x==0 ? 1 : ndigits0z(x)

function ndigits0znb(n::Signed, b::Int)
# The suffix "nb" stands for "negative base"
function ndigits0znb(n::Integer, b::Integer)
# precondition: b < -1 && !(typeof(n) <: Unsigned)
d = 0
while n != 0
n = cld(n,b)
Expand All @@ -357,7 +360,10 @@ function ndigits0znb(n::Signed, b::Int)
return d
end

ndigits0znb(n::Unsigned, b::Integer) = ndigits0znb(signed(n), b)

function ndigits0z(n::Unsigned, b::Int)
# precondition: b > 1
b < 0 && return ndigits0znb(signed(n), b)
b == 2 && return sizeof(n)<<3 - leading_zeros(n)
b == 8 && return (sizeof(n)<<3 - leading_zeros(n) + 2) ÷ 3
Expand All @@ -379,20 +385,23 @@ function ndigits0z(n::Unsigned, b::Int)
end
return d
end
ndigits0z(x::Integer, b::Integer) = ndigits0z(unsigned(abs(x)),Int(b))

ndigitsnb(x::Integer, b::Integer) = x==0 ? 1 : ndigits0znb(x, b)

ndigits(x::Unsigned, b::Integer) = x==0 ? 1 : ndigits0z(x,Int(b))
ndigits(x::Unsigned) = x==0 ? 1 : ndigits0z(x)
ndigits0z(x::Integer, b::Integer) = ndigits0z(unsigned(abs(x)), Int(b))

"""
ndigits(n::Integer, b::Integer=10)
Compute the number of digits in integer `n` written in base `b`.
"""
ndigits(x::Integer, b::Integer) = b >= 0 ? ndigits(unsigned(abs(x)),Int(b)) : ndigitsnb(x, b)
ndigits(x::Integer) = ndigits(unsigned(abs(x)))
function ndigits(x::Integer, b::Integer)
if b < -1
x == 0 ? 1 : ndigits0znb(x, b)
elseif b > 1
x == 0 ? 1 : ndigits0z(x, b)
else
throw(DomainError())
end
end

## integer to string functions ##

Expand Down Expand Up @@ -544,8 +553,7 @@ higher indexes, such that `n == sum([digits[k]*base^(k-1) for k=1:length(digits)
"""
digits(n::Integer, base::T=10, pad::Integer=1) where {T<:Integer} = digits(T, n, base, pad)

function digits(::Type{T}, n::Integer, base::Integer=10, pad::Integer=1) where T<:Integer
2 <= base || throw(ArgumentError("base must be ≥ 2, got $base"))
function digits(T::Type{<:Integer}, n::Integer, base::Integer=10, pad::Integer=1)
digits!(zeros(T, max(pad, ndigits0z(n,base))), n, base)
end

Expand Down
6 changes: 3 additions & 3 deletions test/bigint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ ndigits_mismatch(n) = ndigits(n) != ndigits(BigInt(n))
@test !any(ndigits_mismatch, 8192:9999)

# The following should not crash (#16579)
ndigits(rand(big.(-999:999)), rand(63:typemax(Int)))
ndigits(rand(big.(-999:999)), big(2)^rand(2:999))
ndigits(big(rand(Int)), rand(63:typemax(Int)))
ndigits(big(rand(Int)), big(2)^rand(2:999))

for i in big.([-20:-1;1:20])
for i in big.([-20:-1;1:20; rand(Int)])
for b in -10:1
@test_throws DomainError ndigits(i, b)
end
Expand Down
8 changes: 7 additions & 1 deletion test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ end

@test ndigits(146, -3) == 5

let n = rand(Int)
let (n, b) = rand(Int, 2)
-1 <= b <= 1 && (b = 2) # invalid bases
@test ndigits(n) == ndigits(big(n)) == ndigits(n, 10)
@test ndigits(n, b) == ndigits(big(n), b)
end

for b in -1:1
@test_throws DomainError ndigits(rand(Int), b)
end
@test ndigits(Int8(5)) == ndigits(5)

Expand Down

0 comments on commit b7a9d7d

Please sign in to comment.