Skip to content

Commit

Permalink
Add check for the size of the given timedelta in order to avoid an in…
Browse files Browse the repository at this point in the history
…finite loop
  • Loading branch information
Wooza committed Jun 22, 2022
1 parent 5a89f7d commit 8551005
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Patches and Suggestions
- Oleg Höfling `(hoefling)`_
- Nick Pope `(ngnpope)`_
- Arul Prabakaran `(arulprabakaran)`_
- Florian Kroiß `(Wooza)`_


.. _(lk-geimfari): https://github.com/lk-geimfari
Expand Down Expand Up @@ -158,3 +159,4 @@ Patches and Suggestions
.. _(hoefling): https://github.com/hoefling
.. _(ngnpope): https://github.com/ngnpope
.. _(arulprabakaran): https://github.com/arulprabakaran
.. _(Wooza): https://github.com/Wooza
8 changes: 6 additions & 2 deletions mimesis/providers/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ def bulk_create_datetimes(
:param date_end: End of the range.
:param kwargs: Keyword arguments for :py:class:`datetime.timedelta`
:return: List of datetime objects
:raises: ValueError: When ``date_start``/``date_end`` not passed and
when ``date_start`` larger than ``date_end``.
:raises: ValueError: When ``date_start``/``date_end`` not passed,
when ``date_start`` larger than ``date_end`` or when the given
keywords for `datetime.timedelta` represent a non-positive timedelta.
"""
dt_objects = []

Expand All @@ -66,6 +67,9 @@ def bulk_create_datetimes(
if date_end < date_start:
raise ValueError("date_start can not be larger than date_end")

if timedelta(**kwargs) <= timedelta():
raise ValueError("timedelta must be positive")

while date_start <= date_end:
date_start += timedelta(**kwargs)
dt_objects.append(date_start)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_providers/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def test_bulk_create_datetimes_error(self, _datetime):
with pytest.raises(ValueError):
_datetime.bulk_create_datetimes(None, None)

with pytest.raises(ValueError):
_datetime.bulk_create_datetimes(
date_start, date_start + datetime.timedelta(days=1)
)

def test_year(self, _datetime):
result = _datetime.year(minimum=2000, maximum=_datetime.CURRENT_YEAR)
assert result >= 2000
Expand Down

0 comments on commit 8551005

Please sign in to comment.