From e47abf475e5fadf2f56b57c1d3120c02f2c0c59f Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Sun, 16 Aug 2026 00:09:48 +0530 Subject: [PATCH] Raise TimestampParseError instead of OverflowError for huge timestamps --- srt.py | 10 +++++++++- tests/test_srt.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/srt.py b/srt.py index 905e334..1ec3cf8 100755 --- a/srt.py +++ b/srt.py @@ -250,7 +250,15 @@ def srt_timestamp_to_timedelta(timestamp): if match is None: raise TimestampParseError("Unparseable timestamp: {}".format(timestamp)) hrs, mins, secs, msecs = [int(m) if m else 0 for m in match.groups()] - return timedelta(hours=hrs, minutes=mins, seconds=secs, milliseconds=msecs) + try: + return timedelta(hours=hrs, minutes=mins, seconds=secs, milliseconds=msecs) + except OverflowError: + # timedelta only holds up to ~2.7 million years, so a field with enough + # digits blows past that. This is still a bad timestamp as far as we're + # concerned, so surface it as one rather than leaking OverflowError. + raise TimestampParseError( + "Timestamp is too large to represent: {}".format(timestamp) + ) def sort_and_reindex(subtitles, start_index=1, in_place=False, skip=True): diff --git a/tests/test_srt.py b/tests/test_srt.py index 5e403c4..8cc1065 100644 --- a/tests/test_srt.py +++ b/tests/test_srt.py @@ -688,6 +688,18 @@ def test_bad_timestamp_format_raises(ts): srt.srt_timestamp_to_timedelta(ts) +def test_out_of_range_timestamp_raises(): + # A field big enough to overflow timedelta used to leak OverflowError + with pytest.raises(srt.TimestampParseError): + srt.srt_timestamp_to_timedelta("9999999999999999:00:00,000") + + +def test_parse_out_of_range_timestamp_raises(): + over_range = "1\n9999999999999999:00:00,000 --> 00:00:01,000\nhi\n\n" + with pytest.raises(srt.TimestampParseError): + list(srt.parse(over_range)) + + @given(st.lists(subtitles()), st.lists(st.sampled_from(string.whitespace))) def test_can_parse_index_trailing_ws(input_subs, whitespace): out = ""