Repository navigation
fix: prevent infinite loop in SrtParse on non-numeric trailing content - #933
Merged
Merged
Conversation
SrtParse.parsePart's sequence-number scan looped while looking for the integer cue index, with no end-of-input guard. Once the scanner reaches a non-numeric line at end of input (a trailing junk line, trailing whitespace, or a truncated cue), neither scanUpToCharacters nor scanCharacters advances the position nor yields an Int, so the loop spins forever and hangs the whole subtitle parser. Add an isAtEnd guard to the loop condition, mirroring the guard VTTParse.parsePart already has on its own timestamp-scan loop. After the loop, the existing startString nil-check returns nil, so the outer parse loop exits cleanly. Includes a regression test (testSrtTrailingJunkDoesNotHang) that parses an SRT whose trailing content is not a numeric sequence line and asserts the single valid cue is returned; without the guard the test never returns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SrtParse.parsePartcan hang forever (infinite loop) while parsing SRT input whose trailing content is not a numeric cue index. Reproduces with any of:-->(socanParseaccepts it) but never yields a numeric sequence line.Once this happens the whole subtitle parser never returns.
Root cause
The sequence-number scan in
SrtParse.parseParthas no end-of-input guard:When the scanner is parked on a non-numeric line at end of input,
scanUpToCharacters(from: .newlines)returnsnil(the current character is already a newline / EOF) andscanCharacters(from: .newlines)returnsnil(nothing left to consume). Neither call advances the position, anddecimal.flatMap(Int.init)staysnil, so the loop spins forever.The sibling VTT parser already defends against this —
VTTParse.parsePartterminates its timestamp-scan loop with&& !scanner.isAtEnd; SRT was simply missing the same guard.Fix
Add
&& !scanner.isAtEndto the loop condition (one line). After the loop,scanner.scanUpToString("-->")returnsnilat EOF, the existingif let startString, let endString = …check fails,parsePartreturnsnil, and the outerwhile !scanner.isAtEndloop inparse(scanner:)exits cleanly.Verification
swift buildfor macOS: clean.swift test --filter SubtitleTest: passes (3 cases), including the newtestSrtTrailingJunkDoesNotHangand the existingtestSrt(still 9 cues,parts[8].end == 3601.14— no regression).