From 7297a74235a5c5395b94533f8241295ad2c0f9ea Mon Sep 17 00:00:00 2001 From: Joel Fischer Date: Sun, 13 Nov 2022 05:01:04 -0500 Subject: [PATCH] Fix crash (#94) * When version bytes are out of bounds a crash would occur --- Source/Parse/ID3TagVersionParser.swift | 2 ++ Tests/Parse/ID3TagVersionParserTest.swift | 5 +++++ 2 files changed, 7 insertions(+) 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"))