Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "lyrics_sync",
"name": "Lyrics Sync",
"version": "1.1.3",
"version": "1.1.4",
"private": false,
"nav": {
"label": "Lyrics Sync",
Expand Down
30 changes: 26 additions & 4 deletions routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -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"


Expand Down Expand Up @@ -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"


Expand Down
22 changes: 22 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading