From 53d3e4bbd7e315b6af2f4441c633f9291ba64309 Mon Sep 17 00:00:00 2001 From: imurashov-belkacar Date: Mon, 29 Dec 2025 18:31:18 +0300 Subject: [PATCH 1/2] fix crash on empty txt file --- Sources/FileType/Supported.swift | 2 +- Tests/FileTypeTests/FileTypeTests.swift | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/FileType/Supported.swift b/Sources/FileType/Supported.swift index 165d902..d278e60 100644 --- a/Sources/FileType/Supported.swift +++ b/Sources/FileType/Supported.swift @@ -1679,7 +1679,7 @@ public extension MimeType { return bytes[0...2] == [0x49, 0x49, 0xBC] }), .init(mime: "image/webp", type: .webp, bytesCount: 4, matches: { bytes, _ in - return bytes[8...11] == [0x57, 0x45, 0x42, 0x50] + return bytes.count > 7 && bytes[8...11] == [0x57, 0x45, 0x42, 0x50] }), .init(mime: "image/x-adobe-dng", type: .dng, bytesCount: 4, matches: { bytes, _ in // TODO: Implement magic number check for image/x-adobe-dng diff --git a/Tests/FileTypeTests/FileTypeTests.swift b/Tests/FileTypeTests/FileTypeTests.swift index e84257c..ab254b1 100644 --- a/Tests/FileTypeTests/FileTypeTests.swift +++ b/Tests/FileTypeTests/FileTypeTests.swift @@ -129,3 +129,8 @@ import Foundation } } +@Test func testBlankTxt() async throws { + let data = Data([0x33, 0x2E, 0x31, 0x30, 0x2E, 0x33, 0x0A]) // emty txt file bytes + #expect(FileType.mimeType(data: data) == nil) +} + From 927063c06107e28c5d7f010509d109c4e0725551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=9C=D1=83=D1=80=D0=B0=D1=88?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Mon, 29 Dec 2025 18:32:28 +0300 Subject: [PATCH 2/2] Update Supported.swift --- Sources/FileType/Supported.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/FileType/Supported.swift b/Sources/FileType/Supported.swift index d278e60..5178aa8 100644 --- a/Sources/FileType/Supported.swift +++ b/Sources/FileType/Supported.swift @@ -1679,7 +1679,7 @@ public extension MimeType { return bytes[0...2] == [0x49, 0x49, 0xBC] }), .init(mime: "image/webp", type: .webp, bytesCount: 4, matches: { bytes, _ in - return bytes.count > 7 && bytes[8...11] == [0x57, 0x45, 0x42, 0x50] + return bytes.count > 7 && bytes[8...11] == [0x57, 0x45, 0x42, 0x50] }), .init(mime: "image/x-adobe-dng", type: .dng, bytesCount: 4, matches: { bytes, _ in // TODO: Implement magic number check for image/x-adobe-dng