Skip to content

Commit b349fce

Browse files
author
Simon Pilkington
committed
Fix infinite loop encoding certain types.
1 parent 3294cbd commit b349fce

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

Sources/XMLCoding/Encoder/XMLEncoder.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,17 +907,35 @@ extension _XMLEncoder {
907907
if let boolean = value as? Bool {
908908
return .boolean(boolean)
909909
} else if let string = value as? String {
910-
return .string(string.description)
910+
return .string(string)
911911
} else if let int = value as? Int64 {
912912
return .int64(int)
913913
} else if let int = value as? UInt64 {
914914
return .uint64(int)
915915
} else if let double = value as? Double {
916916
return .double(double)
917+
} else if let double = value as? Float {
918+
return .double(Double(double))
917919
} else if let date = value as? Date {
918920
return try self.box(date)
919921
} else if let data = value as? Data {
920922
return try self.box(data)
923+
} else if let int = value as? Int {
924+
return .int64(Int64(int))
925+
} else if let int = value as? Int8 {
926+
return .int64(Int64(int))
927+
} else if let int = value as? Int16 {
928+
return .int64(Int64(int))
929+
} else if let int = value as? Int32 {
930+
return .int64(Int64(int))
931+
} else if let int = value as? UInt {
932+
return .uint64(UInt64(int))
933+
} else if let int = value as? UInt8 {
934+
return .uint64(UInt64(int))
935+
} else if let int = value as? UInt16 {
936+
return .uint64(UInt64(int))
937+
} else if let int = value as? UInt32 {
938+
return .uint64(UInt64(int))
921939
}
922940

923941
let depth = self.storage.count

Tests/XMLCodingTests/XMLParsingTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class XMLParsingTests: XCTestCase {
4444
let data: Data?
4545
let date: Date?
4646
let bool: Bool?
47+
let int: Int?
4748
let double: Double?
4849

4950
enum CodingKeys: String, CodingKey {
@@ -52,6 +53,7 @@ class XMLParsingTests: XCTestCase {
5253
case data = "Data"
5354
case date = "Date"
5455
case bool = "Bool"
56+
case int = "Int"
5557
case double = "Double"
5658
}
5759
}
@@ -126,6 +128,7 @@ class XMLParsingTests: XCTestCase {
126128
<Data>ZGF0YTE=</Data>
127129
<Date>1534352914</Date>
128130
<Bool>true</Bool>
131+
<Int>77</Int>
129132
<Double>45.345</Double>
130133
</Metadata>
131134
</Response>
@@ -147,6 +150,7 @@ class XMLParsingTests: XCTestCase {
147150

148151
XCTAssertEqual("data1", String(data: response.metadata.data!, encoding: .utf8)) // decode the data
149152
XCTAssertEqual(Date(timeIntervalSince1970: 1534352914), response.metadata.date) // decode the date
153+
XCTAssertEqual(77, response.metadata.int) // decode the integer
150154
XCTAssertEqual(true, response.metadata.bool) // decode the boolean
151155

152156
let double = response.metadata.double!
@@ -252,6 +256,7 @@ class XMLParsingTests: XCTestCase {
252256
<Data></Data>
253257
<Date></Date>
254258
<Bool></Bool>
259+
<Int></Int>
255260
<Double></Double>
256261
</Metadata>
257262
</Response>
@@ -275,6 +280,7 @@ class XMLParsingTests: XCTestCase {
275280
XCTAssertNil(response.metadata.data) // the Data tag is empty and optional
276281
XCTAssertNil(response.metadata.date) // the Date tag is empty and optional
277282
XCTAssertNil(response.metadata.bool) // the Bool tag is empty and optional
283+
XCTAssertNil(response.metadata.int) // the Int tag is empty and optional
278284
XCTAssertNil(response.metadata.double) // the Double tag is empty and optional
279285
}
280286

@@ -325,6 +331,7 @@ class XMLParsingTests: XCTestCase {
325331
<Data />
326332
<Date />
327333
<Bool />
334+
<Int />
328335
<Double />
329336
</Metadata>
330337
</Response>
@@ -348,6 +355,7 @@ class XMLParsingTests: XCTestCase {
348355
XCTAssertNil(response.metadata.data) // the Data tag is empty and optional
349356
XCTAssertNil(response.metadata.date) // the Date tag is empty and optional
350357
XCTAssertNil(response.metadata.bool) // the Bool tag is empty and optional
358+
XCTAssertNil(response.metadata.int) // the Int tag is empty and optional
351359
XCTAssertNil(response.metadata.double) // the Double tag is empty and optional
352360
}
353361

0 commit comments

Comments
 (0)