From d6fcd61c7db5b9bbfa303e41af5f4672250ef497 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 9 Sep 2026 03:09:29 +0000 Subject: [PATCH] fix: a finite-but-huge LRC timestamp could still overflow to OverflowError Same latent bug just found and fixed in the lyrics_karaoke plugin's copy of this code (feedBack-plugin-lyrics-karaoke#9 review): callers gate _lrc_timestamp() calls on math.isfinite(t), but that alone doesn't catch a value like 1e307 -- a real, finite double whose *scaled* form (1e307 * 100 = 1e309) exceeds the max representable double and becomes inf, so round() on the scaled value raised OverflowError instead of the clean skip the finiteness check was meant to guarantee. _lrc_timestamp() now checks the scaled (centisecond) value for finiteness too, raising ValueError instead of letting round() overflow. _format_lrc() and _format_lrc_word_level() now catch that ValueError and skip the segment/word, consistent with their existing malformed- input handling, rather than letting it propagate as a 500. This PR (#11) already merged before this bug was found, so this branch was restarted from main per the merged-PR branch-recovery convention. Bumped plugin.json to 1.1.4. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01HVxQShyx8Mrqe1GKNstjHy --- plugin.json | 2 +- routes.py | 30 ++++++++++++++++++++++++++---- tests/test_helpers.py | 22 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/plugin.json b/plugin.json index b9569e0..d77b2a0 100644 --- a/plugin.json +++ b/plugin.json @@ -1,7 +1,7 @@ { "id": "lyrics_sync", "name": "Lyrics Sync", - "version": "1.1.3", + "version": "1.1.4", "private": false, "nav": { "label": "Lyrics Sync", diff --git a/routes.py b/routes.py index 3394c75..ae56108 100644 --- a/routes.py +++ b/routes.py @@ -191,7 +191,17 @@ def _lrc_timestamp(t: float) -> str: FIRST, then split into minutes/seconds, so the rollover happens before formatting rather than during it. """ - total_centis = round(max(0.0, float(t)) * 100) + seconds = float(t) + # Callers gate on math.isfinite(t) before calling, but that alone + # isn't enough: a value can be finite and still overflow once scaled + # to centiseconds (e.g. 1e307 * 100 = 1e309 exceeds the max + # representable double and becomes inf), which round() below would + # turn into an unhelpful OverflowError instead of the clean skip the + # finiteness check was meant to guarantee. + scaled = max(0.0, seconds) * 100 + if not math.isfinite(scaled): + raise ValueError("LRC timestamp must be finite") + total_centis = round(scaled) minutes, centis = divmod(total_centis, 6000) return f"{minutes:02d}:{centis / 100:05.2f}" @@ -215,7 +225,11 @@ def _format_lrc(segments: list[dict]) -> str: continue if not math.isfinite(t): continue - lines.append(f"[{_lrc_timestamp(t)}]{seg.get('text', '')}") + try: + timestamp = _lrc_timestamp(t) + except ValueError: + continue + lines.append(f"[{timestamp}]{seg.get('text', '')}") return "\n".join(lines) + "\n" @@ -244,9 +258,17 @@ def _format_lrc_word_level(segments: list[dict]) -> str: continue if not math.isfinite(wt): continue - word_parts.append(f"<{_lrc_timestamp(wt)}>{w.get('text', '')}") + try: + word_timestamp = _lrc_timestamp(wt) + except ValueError: + continue + word_parts.append(f"<{word_timestamp}>{w.get('text', '')}") text = " ".join(word_parts) - lines.append(f"[{_lrc_timestamp(t)}]{text}") + try: + timestamp = _lrc_timestamp(t) + except ValueError: + continue + lines.append(f"[{timestamp}]{text}") return "\n".join(lines) + "\n" diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 2265822..df45db3 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -312,3 +312,25 @@ def test_format_lrc_word_level_skips_non_finite_start_and_word_start(): ] lrc = routes._format_lrc_word_level(segments) assert lrc == "[00:00.00]<00:00.50>kept-word\n" + + +def test_lrc_timestamp_rejects_a_finite_value_that_overflows_when_scaled(): + # 1e307 passes math.isfinite() (it's a real, finite double) -- the + # callers' `if not math.isfinite(t): continue` guard doesn't catch it + # -- but 1e307 * 100 = 1e309 exceeds the max representable double and + # becomes inf, so round() on the scaled value used to raise the exact + # OverflowError the finiteness check was meant to prevent. + try: + routes._lrc_timestamp(1e307) + raised = None + except ValueError as exc: + raised = exc + assert raised is not None and "finite" in str(raised) + + +def test_format_lrc_skips_a_finite_value_that_overflows_when_scaled(): + segments = [ + {"start": 1.0, "text": "kept"}, + {"start": 1e307, "text": "overflows-when-scaled dropped"}, + ] + assert routes._format_lrc(segments) == "[00:01.00]kept\n"