Skip to content

Commit

Permalink
Merge branch 'release-candidate' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Jul 8, 2020
2 parents d7cb271 + 5f46d91 commit aebaa30
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy_to_cocoapods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '*'

jobs:
build:
deploy:
runs-on: macOS-latest
steps:
- uses: actions/checkout@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/Packages
/*.xcodeproj
bin/protoc
.swiftpm/
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

3 changes: 2 additions & 1 deletion BinaryCodable.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'BinaryCodable'
s.version = '0.3.0'
s.version = '0.3.1'
s.license = 'Apache 2.0'
s.summary = 'Codable-like interfaces for binary representations.'
s.homepage = 'https://github.com/jverkoey/BinaryCodable'
Expand All @@ -10,6 +10,7 @@ Pod::Spec.new do |s|

s.ios.deployment_target = '13.0'
s.osx.deployment_target = '10.15'
s.swift_versions = ['5.2']

s.source_files = ['Sources/**/*.swift']
end
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.3.1

This patch release fixes a crashing bug introduced in 0.3.0 that occurred when decoding two-byte values from misaligned memory.

# 0.3.0

This minor release adds a `decodeRemainder` method to `BinaryDecodingContainer` and drops support for iOS 12, macOS 10.12-10.14, and Swift 4.
Expand Down
18 changes: 12 additions & 6 deletions Sources/BinaryCodable/BinaryDataCoders/BinaryDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ private class BinaryDataDecodingContainer: BinaryDecodingContainer {
throw BinaryDecodingError.dataCorrupted(.init(debugDescription:
"Not enough data to create a a type of \(type). Needed: \(byteWidth). Received: \(bytes.count)."))
}
let value = bytes.withUnsafeBytes { ptr -> T in
return ptr.load(as: T.self)
return bytes.withUnsafeBytes { ptr in
var value: T = 0
withUnsafeMutableBytes(of: &value) { valuePtr in
valuePtr.copyMemory(from: UnsafeRawBufferPointer(rebasing: ptr[0..<ptr.count]))
}
return value
}
return value
}

func decode<T: FixedWidthInteger>(_ type: T.Type) throws -> T {
Expand All @@ -124,10 +127,13 @@ private class BinaryDataDecodingContainer: BinaryDecodingContainer {
throw BinaryDecodingError.dataCorrupted(.init(debugDescription:
"Not enough data to create a a type of \(type). Needed: \(byteWidth). Received: \(bytes.count)."))
}
let value = bytes.withUnsafeBytes { ptr -> T in
return ptr.load(as: T.self)
return bytes.withUnsafeBytes { ptr in
var value: T = 0
withUnsafeMutableBytes(of: &value) { valuePtr in
valuePtr.copyMemory(from: UnsafeRawBufferPointer(rebasing: ptr[0..<ptr.count]))
}
return value
}
return value
}

func decodeString(encoding: String.Encoding, terminator: UInt8?) throws -> String {
Expand Down
61 changes: 61 additions & 0 deletions Tests/BinaryCodableTests/MisalignedDecoderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020-present the BinaryCodable authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import BinaryCodable
import XCTest

private struct MisalignedUInt16Packet: BinaryDecodable {
let twoByteInt: UInt16
init(from decoder: BinaryDecoder) throws {
var container = decoder.container(maxLength: nil)
_ = try container.decode(length: 1)
self.twoByteInt = try container.decode(UInt16.self)
}
}

private struct MisalignedFloatPacket: BinaryDecodable {
let fourByteFloat: Float
init(from decoder: BinaryDecoder) throws {
var container = decoder.container(maxLength: nil)
_ = try container.decode(length: 1)
self.fourByteFloat = try container.decode(Float.self)
}
}

final class MisalignedDecoderTests: XCTestCase {

func testUInt16() throws {
// Given
let packetData: [UInt8] = [0, 3, 0]
let decoder = BinaryDataDecoder()

// When
let packet = try decoder.decode(MisalignedUInt16Packet.self, from: packetData)

// Then
XCTAssertEqual(packet.twoByteInt, 3)
}

func testFloat() throws {
// Given
let packetData: [UInt8] = [0, 0, 0, 0x80, 0x3f]
let decoder = BinaryDataDecoder()

// When
let packet = try decoder.decode(MisalignedFloatPacket.self, from: packetData)

// Then
XCTAssertEqual(packet.fourByteFloat, 1.0, accuracy: 0.001)
}
}

0 comments on commit aebaa30

Please sign in to comment.