Skip to content

Raise CaptionReadSyntaxError for malformed SRT instead of IndexError/ValueError - #417

Closed
eeshsaxena wants to merge 1 commit into
pbs:mainfrom
eeshsaxena:fix/srt-malformed-timing
Closed

Raise CaptionReadSyntaxError for malformed SRT instead of IndexError/ValueError#417
eeshsaxena wants to merge 1 commit into
pbs:mainfrom
eeshsaxena:fix/srt-malformed-timing

Conversation

@eeshsaxena

Copy link
Copy Markdown

The SRT reader raises bare IndexError/ValueError on some malformed input instead of a CaptionRead* error:

  • SRTReader().detect("1") -> IndexError (reads lines[1] without checking there are two lines)
  • SRTReader().read("1\n00:00:01,000 -->\nHi") -> IndexError from an empty end timestamp
  • a non-numeric timestamp -> ValueError

I made detect return False for content with fewer than two lines, and read validate the --> timing line and wrap timestamp parsing so a malformed timing line raises CaptionReadSyntaxError. Valid SRT parsing is unchanged.

Added tests for single-line detection and malformed timing; they fail on main with IndexError/ValueError and pass with the change, and the rest of the SRT tests still pass. Found it by fuzzing the readers with mutated captions.

…ValueError

SRTReader.detect read lines[1] without checking there were two lines, and
SRTReader.read parsed the '-->' timing line and each timestamp without
validating them. Single-line content, a timing line missing its end
timestamp, or a non-numeric timestamp raised a bare IndexError or ValueError.
detect now returns False for short content and read raises
CaptionReadSyntaxError for a malformed timing line.
@eeshsaxena
eeshsaxena requested a review from a team as a code owner August 15, 2026 02:09
@OlteanuRares

Copy link
Copy Markdown
Contributor

Thanks for this — your later PR #419 covers most of the same ground (and adds a changelog entry + more test cases), so we'll merge that one instead.

One thing #419 is missing that you have here: the detect() bounds check for single-line content. Could you add that two-line fix to #419 as well? Without it, SRTReader().detect("1") still crashes with IndexError.

Closing this one as superseded by #419.

@eeshsaxena

Copy link
Copy Markdown
Author

Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix (Raise CaptionReadSyntaxError for malformed SRT instead of IndexError/ValueError), and it's currently mergeable with no conflicts. No urgency at all, and I'm happy to make any changes you'd like. Thanks for maintaining pycaption!

@OlteanuRares

Copy link
Copy Markdown
Contributor

Hi @eeshsaxena — as mentioned above, this is superseded by your #419. I've just commented there asking for the detect() bounds check to be added. Once that's done we'll merge #419. Closing this one.

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.

2 participants