Skip to content

Raise CaptionReadSyntaxError for malformed SRT instead of IndexError - #419

Merged
OlteanuRares merged 2 commits into
pbs:mainfrom
eeshsaxena:srt-malformed-read-error
Aug 18, 2026
Merged

Raise CaptionReadSyntaxError for malformed SRT instead of IndexError#419
OlteanuRares merged 2 commits into
pbs:mainfrom
eeshsaxena:srt-malformed-read-error

Conversation

@eeshsaxena

Copy link
Copy Markdown

SRTReader().read() leaks a bare IndexError (or ValueError) on a few kinds of malformed SRT, rather than one of the CaptionRead* errors callers expect to catch:

from pycaption import SRTReader
SRTReader().read("1\n")               # IndexError: list index out of range
SRTReader().read("1\n00:x:01,000 --> 00:00:04,000\ntext\n")  # ValueError

The first is a caption number with no timing line after it, the second a non-numeric timestamp. A timing line missing the --> arrow, or a stamp with too few fields, hit the same paths. I guarded the timing-line lookup and wrapped the timestamp parsing so they raise CaptionReadSyntaxError like the other readers do. Added a parametrized test for the malformed cases; full suite still passes.

@eeshsaxena
eeshsaxena requested a review from a team as a code owner August 16, 2026 03:22
@OlteanuRares

Copy link
Copy Markdown
Contributor

Thanks for the fix — this looks good! One small thing before we merge: could you rebase onto the latest main and update the changelog version from 2.3.8 to 2.3.9? (2.3.8 has already been released.)

@eeshsaxena

Copy link
Copy Markdown
Author

@OlteanuRares thanks for the review. Done: rebased onto the latest main and moved the changelog entry into a new 2.3.9 section, since 2.3.8 is already released. The conflict is cleared and it's mergeable again, and the full test suite passes locally (619 passed). Ready whenever you have a chance.

eeshsaxena and others added 2 commits August 18, 2026 12:59
SRTReader.read leaked a raw IndexError or ValueError on a few kinds of
malformed input: a caption number with no timing line after it, a timing
line with no --> arrow, or a timestamp with missing or non-numeric
fields. read() is meant to raise the CaptionRead* errors, so callers
catching those got an unexpected crash instead.

Guard the timing line and wrap the timestamp parsing so these surface as
CaptionReadSyntaxError, matching the other readers.
SRTReader().detect("1") crashed with IndexError because it accessed
lines[1] without checking there were at least two lines. Return False
early when content has fewer than two lines.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@OlteanuRares
OlteanuRares force-pushed the srt-malformed-read-error branch from 3cd759e to 9fb9cd0 Compare August 18, 2026 09:59
@OlteanuRares

Copy link
Copy Markdown
Contributor

Thanks for the rebase! I went ahead and pushed two small changes directly to your branch:

  1. Rebased onto main — picks up Handle malformed SAMI without leaking ValueError/AttributeError #420 (SAMI reader fixes) and resolves the changelog conflict.
  2. Added detect() bounds checkSRTReader().detect("1") was still crashing with IndexError since lines[1] is accessed without a length check. Added the two-line guard from your Raise CaptionReadSyntaxError for malformed SRT instead of IndexError/ValueError #417 plus a parametrized test.

All 24 SRT tests pass. Merging now — thanks for the contribution!

@OlteanuRares
OlteanuRares merged commit 4ec2ef8 into pbs:main Aug 18, 2026
4 checks passed
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.

3 participants