Skip to content

Commit

Permalink
Fix escaped '%' check when building Printf.Format
Browse files Browse the repository at this point in the history
Addresses
JuliaLang#32859 (comment).
The issue here was advancing the parsing position too far when an
escaped `'%'` was encountered after the first format specifier. If an
escaped `'%'` was then followed by another specifier, it was ignored due
to being classified as an "escaped" character.
  • Loading branch information
quinnj committed Sep 8, 2020
1 parent dbd3d4c commit faa81ea
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 1 addition & 3 deletions stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,13 @@ function Format(f::AbstractString)
end
push!(fmts, Spec{type}(leftalign, plus, space, zero, hash, width, precision))
start = pos
prevperc = false
while pos <= len
b = bytes[pos]
pos += 1
if b == UInt8('%')
pos > len && throw(ArgumentError("invalid format string: '$f'"))
if bytes[pos] == UInt8('%')
pos += 1
pos > len && break
# escaped '%'
b = bytes[pos]
pos += 1
else
Expand Down
5 changes: 5 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ end
@test Printf.@sprintf("%f", 1) == "1.000000"
@test Printf.@sprintf("%e", 1) == "1.000000e+00"
@test Printf.@sprintf("%g", 1) == "1"

# escaped '%'
@test_throws ArgumentError @sprintf("%s%%%s", "a")
@test @sprintf("%s%%%s", "a", "b") == "a%%b"

end

@testset "integers" begin
Expand Down

0 comments on commit faa81ea

Please sign in to comment.