Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Printf for typemin(<:Base.BitSigned) #42341

Merged
merged 8 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,13 @@ fmt(buf, pos, arg::AbstractFloat, spec::Spec{T}) where {T <: Ints} =
bs = base(T)
arg2 = toint(arg)
n = i = ndigits(arg2, base=bs, pad=1)
x, neg = arg2 < 0 ? (-arg2, true) : (arg2, false)
one = oneunit(arg2)
neg = arg2 < 0
if typeof(arg2) <: Signed && !(typeof(arg2) <: BigInt)
x = neg ? unsigned(-(arg2+one))+unsigned(one) : unsigned(arg2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about x = unsigned(abs(arg2))? That fixes the problematic case and avoids any conversion problems, since the typemin is handled by unsigned and all other negative values are handled by abs (since they're guaranteed to still fit into the original type).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, I have updated to x = unsigned(neg ? -arg2 : arg2) to be closer to the original code with the same meaning.

else
x = neg ? -arg2 : arg2
end
arglen = n + (neg || (plus | space)) +
(T == Val{'o'} && hash ? 1 : 0) +
(T == Val{'x'} && hash ? 2 : 0) + (T == Val{'X'} && hash ? 2 : 0)
Expand Down
11 changes: 11 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,17 @@ end
@test Printf.@sprintf("%20.0X", UInt(3989525555)) == " EDCB5433"
@test Printf.@sprintf("%20.X", UInt(0)) == " 0"

# issue #41971
@test Printf.@sprintf("%4d", typemin(Int8)) == "-128"
@test Printf.@sprintf("%4d", typemax(Int8)) == " 127"
@test Printf.@sprintf("%6d", typemin(Int16)) == "-32768"
@test Printf.@sprintf("%6d", typemax(Int16)) == " 32767"
@test Printf.@sprintf("%11d", typemin(Int32)) == "-2147483648"
@test Printf.@sprintf("%11d", typemax(Int32)) == " 2147483647"
@test Printf.@sprintf("%20d", typemin(Int64)) == "-9223372036854775808"
@test Printf.@sprintf("%20d", typemax(Int64)) == " 9223372036854775807"
@test Printf.@sprintf("%40d", typemin(Int128)) == "-170141183460469231731687303715884105728"
@test Printf.@sprintf("%40d", typemax(Int128)) == " 170141183460469231731687303715884105727"
end

@testset "%n" begin
Expand Down