Skip to content

Commit

Permalink
Fixed issue where decoding of empty bytes crashed. (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBosecker committed Feb 12, 2021
1 parent f68105b commit 9c7648c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/ExtrasBase64/Chromium.swift
Expand Up @@ -329,6 +329,10 @@ extension Base64 {

@inlinable
public static func decode<Buffer: Collection>(bytes: Buffer, options: DecodingOptions = []) throws -> [UInt8] where Buffer.Element == UInt8 {
guard bytes.count > 0 else {
return []
}

let decoded = try bytes.withContiguousStorageIfAvailable { (input) -> [UInt8] in
let outputLength = ((input.count + 3) / 4) * 3

Expand Down
6 changes: 6 additions & 0 deletions Tests/ExtrasBase64Tests/ChromiumTests.swift
Expand Up @@ -42,6 +42,12 @@ class ChromiumTests: XCTestCase {
XCTAssertEqual(decoded?.count, 0)
}

func testDecodeEmptyBytes() throws {
var decoded: [UInt8]?
XCTAssertNoThrow(decoded = try Base64.decode(bytes: []))
XCTAssertEqual(decoded?.count, 0)
}

func testBase64DecodingArrayOfNulls() throws {
let expected = Array(repeating: UInt8(0), count: 10)
var decoded: [UInt8]?
Expand Down

0 comments on commit 9c7648c

Please sign in to comment.