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

Fixed RecursionError discovered by OSSFuzz #1201

Merged
merged 1 commit into from
Nov 22, 2023
Merged
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
46 changes: 27 additions & 19 deletions dateparser/languages/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,34 @@ def _add_to_cache(self, value, cache):
):
cache.pop(list(cache.keys())[0])

def _split_by_known_words(self, string, keep_formatting):
if not string:
return string

def _split_by_known_words(self, string: str, keep_formatting: bool):
regex = self._get_split_regex_cache()
match = regex.match(string)
if not match:
return (
self._split_by_numerals(string, keep_formatting)
if self._should_capture(string, keep_formatting)
else []
)

unparsed, known, unknown = match.groups()
splitted = [known] if self._should_capture(known, keep_formatting) else []
if unparsed and self._should_capture(unparsed, keep_formatting):
splitted = self._split_by_numerals(unparsed, keep_formatting) + splitted
if unknown:
splitted.extend(self._split_by_known_words(unknown, keep_formatting))

splitted = []
unknown = string

while unknown:
match = regex.match(string)

if not match:
curr_split = (
self._split_by_numerals(string, keep_formatting)
if self._should_capture(string, keep_formatting)
else []
)
unknown = ""
else:
unparsed, known, unknown = match.groups()
curr_split = (
[known] if self._should_capture(known, keep_formatting) else []
)
if unparsed and self._should_capture(unparsed, keep_formatting):
curr_split = (
self._split_by_numerals(unparsed, keep_formatting) + curr_split
)
if unknown:
string = unknown if string != unknown else ""

splitted.extend(curr_split)
return splitted

def _split_by_numerals(self, string, keep_formatting):
Expand Down