Skip to content

Handle malformed SAMI without leaking ValueError/AttributeError - #420

Open
eeshsaxena wants to merge 1 commit into
pbs:mainfrom
eeshsaxena:sami-malformed-timing
Open

Handle malformed SAMI without leaking ValueError/AttributeError#420
eeshsaxena wants to merge 1 commit into
pbs:mainfrom
eeshsaxena:sami-malformed-timing

Conversation

@eeshsaxena

Copy link
Copy Markdown

Two kinds of malformed SAMI make SAMIReader().read() leak a raw exception instead of one of the CaptionRead* errors:

from pycaption import SAMIReader
SAMIReader().read("<SAMI><BODY><SYNC Start=abc><P>hi</P></SYNC></BODY></SAMI>")  # ValueError
SAMIReader().read("<SAMI><BODY><SYNC Start=1000><P Class>hi</P></SYNC></BODY></SAMI>")  # AttributeError

The first is a non-numeric start attribute hitting int(float(start)). The second is a valueless class attribute (HTMLParser gives it a value of None), which _find_lang then calls .lower() on. A bare lang attribute trips the same None path via value[:2].

Non-numeric start now raises CaptionReadTimingError, matching the missing-start check right next to it. A valueless class/lang attribute carries no language info, so it's just skipped. Added tests for both; the SAMI suite and full suite still pass.

SAMIReader leaked two raw exceptions on malformed input. A <sync> tag
with a non-numeric start attribute (e.g. start=abc) blew up in
int(float(start)) with ValueError, and a tag with a valueless class or
lang attribute (e.g. <p class>) hit value.lower()/value[:2] on None and
raised AttributeError/TypeError in _find_lang.

Non-numeric start now raises CaptionReadTimingError like the missing
start case next to it, and a valueless class/lang attribute is skipped
since it carries no language info.
@eeshsaxena
eeshsaxena requested a review from a team as a code owner August 16, 2026 03:26
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