diff --git a/Source/Parse/ID3TagVersionParser.swift b/Source/Parse/ID3TagVersionParser.swift index b5557cc3..3a1c674f 100644 --- a/Source/Parse/ID3TagVersionParser.swift +++ b/Source/Parse/ID3TagVersionParser.swift @@ -19,6 +19,8 @@ class ID3TagVersionParser: TagVersionParser { } private func tryToGetVersionFrom(mp3: Data) -> ID3Version? { + guard mp3.count > versionBytesOffset else { return nil } + let version = [UInt8](mp3)[versionBytesOffset] return ID3Version(rawValue: version) } diff --git a/Tests/Parse/ID3TagVersionParserTest.swift b/Tests/Parse/ID3TagVersionParserTest.swift index 6f440de1..d112c314 100644 --- a/Tests/Parse/ID3TagVersionParserTest.swift +++ b/Tests/Parse/ID3TagVersionParserTest.swift @@ -25,6 +25,11 @@ class ID3TagVersionParserTest: XCTestCase { XCTAssertEqual(.version3, id3VersionParser.parse(mp3: mp3WithV2Tag)) } + func testShortData() throws { + let shortData = Data(capacity: 2) + XCTAssertEqual(.version3, id3VersionParser.parse(mp3: shortData)) + } + func testDefaultVersion() throws { let mp3WithV2Tag = try Data( contentsOf: URL(fileURLWithPath: PathLoader().pathFor(name: "example-to-be-modified", fileType: "mp3"))