Skip to content

Fix Date parsing crash when parsing a negative year #53981

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

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion stdlib/Dates/src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ end
min_pos = min_width <= 0 ? i : i + min_width - 1
max_pos = max_width <= 0 ? len : min(i + max_width - 1, len)
d::Int64 = 0
c, neg = iterate(str, i)::Tuple{Char, Int}
if c == '-'
i = neg
neg = -1
else
neg = 1
end
@inbounds while i <= max_pos
c, ii = iterate(str, i)::Tuple{Char, Int}
if '0' <= c <= '9'
Expand All @@ -173,7 +180,7 @@ end
if i <= min_pos
return nothing
else
return d, i
return d * neg, i
end
end

Expand Down
5 changes: 5 additions & 0 deletions stdlib/Dates/test/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,9 @@ end
end
end

@testset "Issue #50328: parsing negative years" begin
@test Date("-2013-10-10") == Date(-2013, 10, 10)
@test Date("-2013") == Date(-2013, 01, 01)
end

end