Skip to content

Commit 9c7648c

Browse files
authored
Fixed issue where decoding of empty bytes crashed. (#27)
1 parent f68105b commit 9c7648c

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Sources/ExtrasBase64/Chromium.swift

+4
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ extension Base64 {
329329

330330
@inlinable
331331
public static func decode<Buffer: Collection>(bytes: Buffer, options: DecodingOptions = []) throws -> [UInt8] where Buffer.Element == UInt8 {
332+
guard bytes.count > 0 else {
333+
return []
334+
}
335+
332336
let decoded = try bytes.withContiguousStorageIfAvailable { (input) -> [UInt8] in
333337
let outputLength = ((input.count + 3) / 4) * 3
334338

Tests/ExtrasBase64Tests/ChromiumTests.swift

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class ChromiumTests: XCTestCase {
4242
XCTAssertEqual(decoded?.count, 0)
4343
}
4444

45+
func testDecodeEmptyBytes() throws {
46+
var decoded: [UInt8]?
47+
XCTAssertNoThrow(decoded = try Base64.decode(bytes: []))
48+
XCTAssertEqual(decoded?.count, 0)
49+
}
50+
4551
func testBase64DecodingArrayOfNulls() throws {
4652
let expected = Array(repeating: UInt8(0), count: 10)
4753
var decoded: [UInt8]?

0 commit comments

Comments
 (0)