Skip to content

Commit aaa3093

Browse files
committed
Fixed bug #13 (#13)
Code make an assumption as to the number of elements in an array. The input file is _very_ oddly formatted which triggered the original crash (which is now fixed)
1 parent c1fcc2a commit aaa3093

File tree

3 files changed

+429
-7
lines changed

3 files changed

+429
-7
lines changed

Sources/SwiftSubtitles/coding/VTT.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public extension Subtitles.Coder.VTT {
123123
.removingBOM()
124124
.lines
125125
.enumerated()
126-
.map { (offset: $0.offset, element: $0.element.trimmingCharacters(in: .whitespaces)) }
126+
.map { (offset: $0.offset, element: $0.element) }
127127

128128
guard lines[0].element.contains("WEBVTT") else {
129129
throw SubTitlesError.invalidFile
@@ -135,7 +135,7 @@ public extension Subtitles.Coder.VTT {
135135
var inSection = false
136136
var currentLines = [(index: Int, line: String)]()
137137
lines.forEach { item in
138-
let line = item.element.trimmingCharacters(in: .whitespaces)
138+
let line = item.element
139139
if line.isEmpty {
140140
if inSection == true {
141141
// End of section
@@ -229,10 +229,20 @@ public extension Subtitles.Coder.VTT {
229229
// Assume its a cue identifier
230230
identifier = l1.line
231231

232+
// Next line should be the times
232233
index += 1
233-
let l2 = section[index]
234-
times = try parseTime(index: l2.index, timeLine: l2.line)
235-
index += 1
234+
235+
if index < section.count {
236+
let l2 = section[index]
237+
times = try parseTime(index: l2.index, timeLine: l2.line)
238+
index += 1
239+
}
240+
}
241+
242+
// Check if we've found a time value. If not, then skip it
243+
guard let times = times else {
244+
// WebVTT format error? Cannot find time definition
245+
continue
236246
}
237247

238248
// next is the text
@@ -264,8 +274,8 @@ public extension Subtitles.Coder.VTT {
264274

265275
let entry = Subtitles.Cue(
266276
identifier: identifier,
267-
startTime: times!.0,
268-
endTime: times!.1,
277+
startTime: times.0,
278+
endTime: times.1,
269279
text: text,
270280
speaker: speaker
271281
)

Tests/SwiftSubtitlesTests/BugTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,17 @@ final class BugTests: XCTestCase {
4242
XCTAssertFalse(st.cues[1].isZeroLength())
4343
XCTAssertEqual(0.001, st.cues[1].duration, accuracy: 0.0001)
4444
}
45+
46+
func testCrash13() throws {
47+
// https://github.com/dagronf/SwiftSubtitles/issues/13
48+
// This file crashed the VTT importer.
49+
// It's a very odd file (but appears to be valid vtt) and I'm guessing its AI generated with lots of
50+
// duplicate lines, cues containing only a single space.
51+
// The issue was that I was trimming lines of whitespace, which would nuke some of the
52+
// cue entries that contained only a single space (?). The code logic made an assumption
53+
// regarding the length of an array and as such busted.
54+
let fileURL = Bundle.module.url(forResource: "crash_13", withExtension: "vtt")!
55+
let subtitles = try Subtitles(fileURL: fileURL, encoding: .utf8)
56+
XCTAssertEqual(99, subtitles.cues.count)
57+
}
4558
}

0 commit comments

Comments
 (0)