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"