Skip to content

Commit 42d8db0

Browse files
committed
improve decoder error hanlding
1 parent c16afc2 commit 42d8db0

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

Codec/Sources/Codec/DataInput.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ import Foundation
33
public protocol DataInput {
44
/// read some data chunk
55
/// throw when no more data
6-
mutating func read(length: Int) throws -> Data
6+
mutating func read(length: Int, codingPath: @autoclosure () -> [CodingKey]) throws -> Data
77

88
mutating func readAll() throws -> Data
99

1010
var isEmpty: Bool { get }
1111
}
1212

1313
extension DataInput {
14-
public mutating func read() throws -> UInt8 {
15-
try read(length: 1).first!
14+
public mutating func read(_ codingPath: @autoclosure () -> [CodingKey]) throws -> UInt8 {
15+
try read(length: 1, codingPath: codingPath()).first!
1616
}
1717

1818
public mutating func readAll() throws -> Data {
1919
try readAll()
2020
}
2121

22-
public mutating func decodeUInt64() throws -> UInt64 {
22+
public mutating func decodeUInt64(_ codingPath: @autoclosure () -> [CodingKey]) throws -> UInt64 {
2323
// TODO: improve this by use `read(minLength: 8)` to avoid read byte by byte
24-
let res = try IntegerCodec.decode { try self.read() }
24+
let res = try IntegerCodec.decode { try self.read(codingPath()) }
2525
guard let res else {
2626
throw DecodingError.dataCorrupted(
2727
DecodingError.Context(
28-
codingPath: [],
28+
codingPath: codingPath(),
2929
debugDescription: "Not enough data to perform variable length integer decoding"
3030
)
3131
)
@@ -35,11 +35,11 @@ extension DataInput {
3535
}
3636

3737
extension Data: DataInput {
38-
public mutating func read(length: Int) throws -> Data {
38+
public mutating func read(length: Int, codingPath: @autoclosure () -> [CodingKey]) throws -> Data {
3939
guard count >= length else {
4040
throw DecodingError.dataCorrupted(
4141
DecodingError.Context(
42-
codingPath: [],
42+
codingPath: codingPath(),
4343
debugDescription: "Not enough data to decode \(length) bytes"
4444
)
4545
)

Codec/Sources/Codec/JamDecoder.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ private class DecodeContext: Decoder {
113113
JamSingleValueDecodingContainer(codingPath: codingPath, decoder: self)
114114
}
115115

116-
fileprivate func decodeInt<T: FixedWidthInteger>(codingPath _: @autoclosure () -> [CodingKey]) throws -> T {
117-
let data = try input.read(length: MemoryLayout<T>.size)
116+
fileprivate func decodeInt<T: FixedWidthInteger>(codingPath: @autoclosure () -> [CodingKey]) throws -> T {
117+
let data = try input.read(length: MemoryLayout<T>.size, codingPath: codingPath())
118118
return data.withUnsafeBytes { ptr in
119119
ptr.loadUnaligned(as: T.self)
120120
}
121121
}
122122

123123
fileprivate func decodeCompact<T: FixedWidthInteger>(codingPath: @autoclosure () -> [CodingKey]) throws -> T {
124-
let value = try input.decodeUInt64()
124+
let value = try input.decodeUInt64(codingPath())
125125
guard let res = T(exactly: value) else {
126126
throw DecodingError.dataCorrupted(
127127
DecodingError.Context(
@@ -138,7 +138,7 @@ private class DecodeContext: Decoder {
138138
if path.isEmpty {
139139
return try input.readAll()
140140
}
141-
let length = try input.decodeUInt64()
141+
let length = try input.decodeUInt64(path)
142142
// sanity check: length must be less than 4gb
143143
guard length < 0x1_0000_0000 else {
144144
throw DecodingError.dataCorrupted(
@@ -148,7 +148,7 @@ private class DecodeContext: Decoder {
148148
)
149149
)
150150
}
151-
let res = try input.read(length: Int(length))
151+
let res = try input.read(length: Int(length), codingPath: path)
152152
return res
153153
}
154154

@@ -157,7 +157,7 @@ private class DecodeContext: Decoder {
157157
if path.isEmpty {
158158
return try [UInt8](input.readAll())
159159
}
160-
let length = try input.decodeUInt64()
160+
let length = try input.decodeUInt64(path)
161161
// sanity check: length must be less than 4gb
162162
guard length < 0x1_0000_0000 else {
163163
throw DecodingError.dataCorrupted(
@@ -167,12 +167,12 @@ private class DecodeContext: Decoder {
167167
)
168168
)
169169
}
170-
let res = try input.read(length: Int(length))
170+
let res = try input.read(length: Int(length), codingPath: path)
171171
return Array(res)
172172
}
173173

174174
fileprivate func decodeArray<T: ArrayWrapper>(_ type: T.Type, key: CodingKey?) throws -> T {
175-
let length = try input.decodeUInt64()
175+
let length = try input.decodeUInt64(codingPath)
176176
// sanity check: length can't be unreasonably large
177177
guard length < 0xFFFFFF else {
178178
throw DecodingError.dataCorrupted(
@@ -193,13 +193,13 @@ private class DecodeContext: Decoder {
193193
fileprivate func decodeFixedLengthData<T: FixedLengthData>(_ type: T.Type, key: CodingKey?) throws -> T {
194194
try withExtendedLifetime(PushCodingPath(decoder: self, key: key)) {
195195
let length = try type.length(decoder: self)
196-
let data = try input.read(length: length)
196+
let data = try input.read(length: length, codingPath: codingPath)
197197
return try type.init(decoder: self, data: data)
198198
}
199199
}
200200

201201
fileprivate func decodeOptional<T: Decodable>(_ type: T.Type, key: CodingKey?) throws -> T? {
202-
let byte = try input.read()
202+
let byte = try input.read(codingPath)
203203
switch byte {
204204
case 0:
205205
return nil
@@ -251,12 +251,12 @@ private struct JamKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerPr
251251
}
252252

253253
func decodeNil(forKey _: K) throws -> Bool {
254-
let byte = try decoder.input.read()
254+
let byte = try decoder.input.read(codingPath)
255255
return byte == 0
256256
}
257257

258258
func decode(_: Bool.Type, forKey _: K) throws -> Bool {
259-
let byte = try decoder.input.read()
259+
let byte = try decoder.input.read(codingPath)
260260
return byte == 1
261261
}
262262

@@ -283,8 +283,8 @@ private struct JamKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerPr
283283
return value
284284
}
285285

286-
func decode(_: Int8.Type, forKey _: K) throws -> Int8 {
287-
let byte = try decoder.input.read()
286+
func decode(_: Int8.Type) throws -> Int8 {
287+
let byte = try decoder.input.read(codingPath)
288288
return Int8(bitPattern: byte)
289289
}
290290

@@ -305,7 +305,7 @@ private struct JamKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerPr
305305
}
306306

307307
func decode(_: UInt8.Type, forKey _: K) throws -> UInt8 {
308-
let byte = try decoder.input.read()
308+
let byte = try decoder.input.read(codingPath)
309309
return byte
310310
}
311311

@@ -326,7 +326,7 @@ private struct JamKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerPr
326326
}
327327

328328
func decodeIfPresent<T: Decodable>(_ type: T.Type, forKey key: K) throws -> T? {
329-
let byte = try decoder.input.read()
329+
let byte = try decoder.input.read(codingPath)
330330
switch byte {
331331
case 0:
332332
return nil
@@ -373,13 +373,13 @@ private struct JamUnkeyedDecodingContainer: UnkeyedDecodingContainer {
373373
let decoder: DecodeContext
374374

375375
mutating func decodeNil() throws -> Bool {
376-
let byte = try decoder.input.read()
376+
let byte = try decoder.input.read(codingPath)
377377
currentIndex += 1
378378
return byte == 0
379379
}
380380

381381
mutating func decode(_: Bool.Type) throws -> Bool {
382-
let byte = try decoder.input.read()
382+
let byte = try decoder.input.read(codingPath)
383383
currentIndex += 1
384384
return byte == 1
385385
}
@@ -410,7 +410,7 @@ private struct JamUnkeyedDecodingContainer: UnkeyedDecodingContainer {
410410
}
411411

412412
mutating func decode(_: Int8.Type) throws -> Int8 {
413-
let byte = try decoder.input.read()
413+
let byte = try decoder.input.read(codingPath)
414414
currentIndex += 1
415415
return Int8(bitPattern: byte)
416416
}
@@ -437,7 +437,7 @@ private struct JamUnkeyedDecodingContainer: UnkeyedDecodingContainer {
437437
}
438438

439439
mutating func decode(_: UInt8.Type) throws -> UInt8 {
440-
let byte = try decoder.input.read()
440+
let byte = try decoder.input.read(codingPath)
441441
currentIndex += 1
442442
return byte
443443
}
@@ -483,12 +483,12 @@ private struct JamSingleValueDecodingContainer: SingleValueDecodingContainer {
483483
let decoder: DecodeContext
484484

485485
func decodeNil() -> Bool {
486-
let byte = try? decoder.input.read()
486+
let byte = try? decoder.input.read(codingPath)
487487
return byte == 0
488488
}
489489

490490
func decode(_: Bool.Type) throws -> Bool {
491-
let byte = try decoder.input.read()
491+
let byte = try decoder.input.read(codingPath)
492492
return byte == 1
493493
}
494494

@@ -516,7 +516,7 @@ private struct JamSingleValueDecodingContainer: SingleValueDecodingContainer {
516516
}
517517

518518
func decode(_: Int8.Type) throws -> Int8 {
519-
let byte = try decoder.input.read()
519+
let byte = try decoder.input.read(codingPath)
520520
return Int8(bitPattern: byte)
521521
}
522522

@@ -537,7 +537,7 @@ private struct JamSingleValueDecodingContainer: SingleValueDecodingContainer {
537537
}
538538

539539
func decode(_: UInt8.Type) throws -> UInt8 {
540-
let byte = try decoder.input.read()
540+
let byte = try decoder.input.read(codingPath)
541541
return byte
542542
}
543543

Tools/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let package = Package(
1414
.package(path: "../TracingUtils"),
1515
.package(path: "../Utils"),
1616
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
17-
.package(url: "https://github.com/ajevans99/swift-json-schema.git", from: "0.10.0"),
17+
.package(url: "https://github.com/ajevans99/swift-json-schema.git", revision: "v0.10.0"),
1818
.package(url: "https://github.com/wickwirew/Runtime.git", from: "2.2.7"),
1919
],
2020
targets: [

Utils/Tests/UtilsTests/Crypto/JamTestnetSeedTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct JamTestnetSeedTests {
1212
#expect(bandersnatch.publicKey.data.toHexString() == "5e465beb01dbafe160ce8216047f2155dd0569f058afd52dcea601025a8d161d")
1313
#expect(ed25519.publicKey.data.toHexString() == "3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29")
1414
#expect(bls.publicKey.data.toHexString() ==
15-
// swiftlint:disable:next line_length
1615
"b27150a1f1cd24bccc792ba7ba4220a1e8c36636e35a969d1d14b4c89bce7d1d463474fb186114a89dd70e88506fefc9830756c27a7845bec1cb6ee31e07211afd0dde34f0dc5d89231993cd323973faa23d84d521fd574e840b8617c75d1a1d0102aa3c71999137001a77464ced6bb2885c460be760c709009e26395716a52c8c52e6e23906a455b4264e7d0c75466e")
1716
}
1817

@@ -24,7 +23,6 @@ struct JamTestnetSeedTests {
2423
#expect(bandersnatch.publicKey.data.toHexString() == "1565283e47871f63e863b3c78dc82c9a62ad3040027a7996c827e2461cbf1571")
2524
#expect(ed25519.publicKey.data.toHexString() == "4cb5abf6ad79fbf5abbccafcc269d85cd2651ed4b885b5869f241aedf0a5ba29")
2625
#expect(bls.publicKey.data.toHexString() ==
27-
// swiftlint:disable:next line_length
2826
"8b8a096ada14a51df7e2067007bf6c24d7568d88bf89816c1287ba2784b4188c3536d70b1a1cbc8ab438056e457e2aa0ab48d30d6279373652d19269f7260624d0965c3dc00ed944d1b6ff6db06bb73dc1314164e9fed6020108487897ac3a9814eca841aedc47f504a848513166ffe39f89c9f3e7c6729dc99207f863a10bda142d5a24ba42b90d99d6d6df3fa6d780")
2927
}
3028

@@ -36,7 +34,6 @@ struct JamTestnetSeedTests {
3634
#expect(bandersnatch.publicKey.data.toHexString() == "699a8fcb24649d8f159a5bd11916cb9541dc5360690c06935ecdf3b6d06cce01")
3735
#expect(ed25519.publicKey.data.toHexString() == "7422b9887598068e32c4448a949adb290d0f4e35b9e01b0ee5f1a1e600fe2674")
3836
#expect(bls.publicKey.data.toHexString() ==
39-
// swiftlint:disable:next line_length
4037
"93377fa4dddd7cf95dddef8edfe9ff310ba4d8dffa57e34f2774ad2a6adb16e8ebca12e037dcaf5d762d8eaa9b9cb40498b771e65d8364b1af4cbf51b41525df62b78d8507218c14d9af1eeb96bec770646b9f2b887518b3248f8d8d526874231255aa247b7e252c0802be0a91cc659a0f4b679487345ab8a5f5d67b53319d6ad7d946b9976be4deab9e9a7f2486ecb1")
4138
}
4239
}

0 commit comments

Comments
 (0)