Skip to content

Commit

Permalink
Fix Date parsing crash when parsing a negative year (#53981)
Browse files Browse the repository at this point in the history
Fixes #50328.
Originally checked on version 1.10.0 but still relevant in the current
version in master

Bug: In method Date(str), when given a negative year as an argument (ex:
"-2000"), it would output an error while in Date(int) once given a
negative year (ex: "-2000") it would work as intended returning
"-2000-01-01".

Fix: In function tryparsenext_base10 the character '-' wasn't being
accounted so i check if it existed in the first iteration of the string
and if yes I would multiply the output * -1.

Test: Added some tests that verify this specific Fix.

---------

Co-authored-by: Jameson Nash <[email protected]>
Co-authored-by: Viral B. Shah <[email protected]>
  • Loading branch information
3 people authored May 21, 2024
1 parent 98f3947 commit 4366a93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

2 comments on commit 4366a93

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.