Skip to content

Commit aebaa30

Browse files
committed
Merge branch 'release-candidate' into stable
2 parents d7cb271 + 5f46d91 commit aebaa30

File tree

7 files changed

+81
-9
lines changed

7 files changed

+81
-9
lines changed

.github/workflows/deploy_to_cocoapods.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- '*'
77

88
jobs:
9-
build:
9+
deploy:
1010
runs-on: macOS-latest
1111
steps:
1212
- uses: actions/checkout@v1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/Packages
44
/*.xcodeproj
55
bin/protoc
6+
.swiftpm/

.swift-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

BinaryCodable.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'BinaryCodable'
3-
s.version = '0.3.0'
3+
s.version = '0.3.1'
44
s.license = 'Apache 2.0'
55
s.summary = 'Codable-like interfaces for binary representations.'
66
s.homepage = 'https://github.com/jverkoey/BinaryCodable'
@@ -10,6 +10,7 @@ Pod::Spec.new do |s|
1010

1111
s.ios.deployment_target = '13.0'
1212
s.osx.deployment_target = '10.15'
13+
s.swift_versions = ['5.2']
1314

1415
s.source_files = ['Sources/**/*.swift']
1516
end

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.3.1
2+
3+
This patch release fixes a crashing bug introduced in 0.3.0 that occurred when decoding two-byte values from misaligned memory.
4+
15
# 0.3.0
26

37
This minor release adds a `decodeRemainder` method to `BinaryDecodingContainer` and drops support for iOS 12, macOS 10.12-10.14, and Swift 4.

Sources/BinaryCodable/BinaryDataCoders/BinaryDataDecoder.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ private class BinaryDataDecodingContainer: BinaryDecodingContainer {
111111
throw BinaryDecodingError.dataCorrupted(.init(debugDescription:
112112
"Not enough data to create a a type of \(type). Needed: \(byteWidth). Received: \(bytes.count)."))
113113
}
114-
let value = bytes.withUnsafeBytes { ptr -> T in
115-
return ptr.load(as: T.self)
114+
return bytes.withUnsafeBytes { ptr in
115+
var value: T = 0
116+
withUnsafeMutableBytes(of: &value) { valuePtr in
117+
valuePtr.copyMemory(from: UnsafeRawBufferPointer(rebasing: ptr[0..<ptr.count]))
118+
}
119+
return value
116120
}
117-
return value
118121
}
119122

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

133139
func decodeString(encoding: String.Encoding, terminator: UInt8?) throws -> String {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020-present the BinaryCodable authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import BinaryCodable
16+
import XCTest
17+
18+
private struct MisalignedUInt16Packet: BinaryDecodable {
19+
let twoByteInt: UInt16
20+
init(from decoder: BinaryDecoder) throws {
21+
var container = decoder.container(maxLength: nil)
22+
_ = try container.decode(length: 1)
23+
self.twoByteInt = try container.decode(UInt16.self)
24+
}
25+
}
26+
27+
private struct MisalignedFloatPacket: BinaryDecodable {
28+
let fourByteFloat: Float
29+
init(from decoder: BinaryDecoder) throws {
30+
var container = decoder.container(maxLength: nil)
31+
_ = try container.decode(length: 1)
32+
self.fourByteFloat = try container.decode(Float.self)
33+
}
34+
}
35+
36+
final class MisalignedDecoderTests: XCTestCase {
37+
38+
func testUInt16() throws {
39+
// Given
40+
let packetData: [UInt8] = [0, 3, 0]
41+
let decoder = BinaryDataDecoder()
42+
43+
// When
44+
let packet = try decoder.decode(MisalignedUInt16Packet.self, from: packetData)
45+
46+
// Then
47+
XCTAssertEqual(packet.twoByteInt, 3)
48+
}
49+
50+
func testFloat() throws {
51+
// Given
52+
let packetData: [UInt8] = [0, 0, 0, 0x80, 0x3f]
53+
let decoder = BinaryDataDecoder()
54+
55+
// When
56+
let packet = try decoder.decode(MisalignedFloatPacket.self, from: packetData)
57+
58+
// Then
59+
XCTAssertEqual(packet.fourByteFloat, 1.0, accuracy: 0.001)
60+
}
61+
}

0 commit comments

Comments
 (0)