Skip to content

Commit

Permalink
BigInt printf fixes
Browse files Browse the repository at this point in the history
Relative and octal printing used incorrect arguments. Fixes JuliaLang#36031.
  • Loading branch information
simonbyrne committed May 27, 2020
1 parent 052315f commit ddc65bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 10 additions & 9 deletions stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -942,10 +942,9 @@ function decode_0ct(x::BigInt, digits)
pt = Base.ndigits0z(x, 8) + 1
length(digits) < pt+1 && resize!(digits, pt+1)
neg && (x.size = -x.size)
p = convert(Ptr{UInt8}, digits) + 1
GMP.MPZ.get_str!(p, 8, x)
GMP.MPZ.get_str!(pointer(digits, 2), 8, x)
neg && (x.size = -x.size)
return neg, Int32(pt), Int32(pt)
return Int32(pt), Int32(pt), neg
end

### decoding functions directly used by printf generated code ###
Expand Down Expand Up @@ -1059,13 +1058,15 @@ function ini_dec(x::BigInt, n::Int, digits)
end
d = Base.ndigits0z(x)
if d <= n
info = decode_dec(x)
d == n && return info
p = convert(Ptr{Cvoid}, digits) + info[2]
ccall(:memset, Ptr{Cvoid}, (Ptr{Cvoid}, Cint, Csize_t), p, '0', n - info[2])
return info
len,pt,neg = decode_dec(x, digits)
if d < n
ccall(:memset, Ptr{Cvoid}, (Ptr{Cvoid}, Cint, Csize_t), pointer(digits, pt+1), '0', n - pt)
end
return (len,pt,neg)
else
_,_,neg = decode_dec(round(BigInt,x/big(10)^(d-n)), digits)
return (n, d, neg)
end
return (n, d, decode_dec(round(BigInt,x/big(10)^(d-n)))[3])
end


Expand Down
5 changes: 2 additions & 3 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ for (fmt, val) in (("%i", "42"),
("%20a"," 0x2.ap+4"),
("%-20a","0x2.ap+4 "),
("%f", "42.000000"),
("%g", "42")),
("%g", "42"),
("%e", "4.200000e+01")),
num in (UInt16(42), UInt32(42), UInt64(42), UInt128(42),
Int16(42), Int32(42), Int64(42), Int128(42), big"42")
#big"42" causes stack overflow on %a ; gh #14409
num isa BigInt && fmt in ["%a", "%#o", "%g"] && continue
@test @eval(@sprintf($fmt, $num) == $val)
end

Expand Down

0 comments on commit ddc65bb

Please sign in to comment.