Skip to content

Return None instead of raising OverflowError on an out-of-range value - #32

Open
eeshsaxena wants to merge 1 commit into
wroberts:masterfrom
eeshsaxena:fix/timeparse-overflow
Open

Return None instead of raising OverflowError on an out-of-range value#32
eeshsaxena wants to merge 1 commit into
wroberts:masterfrom
eeshsaxena:fix/timeparse-overflow

Conversation

@eeshsaxena

Copy link
Copy Markdown

I was feeding some messy strings through timeparse and hit a crash on a very large number:

>>> from pytimeparse import timeparse
>>> timeparse('9' * 400 + '.5 minutes')
Traceback (most recent call last):
  ...
OverflowError: cannot convert float infinity to integer

The value is syntactically fine, it is just enormous. float() on it overflows to inf, and the integer-returning branch then does int(sum(...)), which cannot convert inf and raises OverflowError.

The function already handles a malformed number field: a string like '1.2.3 seconds' throws ValueError from float(), which is caught so the parser moves on and ultimately returns None. The out-of-range case is the same situation, the field just is not a usable number, but OverflowError was not in the except, so it escaped instead of falling through to None.

The fix is to catch OverflowError alongside ValueError in that block. After it, '9' * 400 + '.5 minutes' returns None like any other input that cannot be parsed, and normal values are untouched.

I added a test for the out-of-range case and the full suite still passes (50 tests).

timeparse already skips a malformed number field (like '1.2.3') and moves
on to return None, but only ValueError was caught. A value large enough that
float() overflows to inf makes int(sum(...)) raise OverflowError, which
escaped and crashed the call. Catch OverflowError alongside ValueError so
these behave like any other unparseable input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant