Skip to content

Handle malformed SAMI without leaking ValueError/AttributeError - #420

Merged
OlteanuRares merged 1 commit into
pbs:mainfrom
eeshsaxena:sami-malformed-timing
Aug 18, 2026
Merged

Handle malformed SAMI without leaking ValueError/AttributeError#420
OlteanuRares merged 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.

@eeshsaxena
eeshsaxena requested a review from a team as a code owner August 16, 2026 03:26
@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.)

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 force-pushed the sami-malformed-timing branch from bb9e990 to c2e8cfe Compare August 18, 2026 08:03
@eeshsaxena

Copy link
Copy Markdown
Author

@OlteanuRares thanks for the review. Done: rebased onto the latest main and moved the two changelog entries 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 (618 passed). Ready whenever you have a chance.

@OlteanuRares

Copy link
Copy Markdown
Contributor

Thanks @eeshsaxena! Looks good, merging now.

@OlteanuRares
OlteanuRares merged commit 7a07d6b 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.

2 participants