Skip to content

Commit 1eed9aa

Browse files
authored
Revert "Fix #1176 JSON Decoder and Encoder limit disagreement (#1242)" (#1306)
This reverts commit 0f62460.
1 parent 4dc19f0 commit 1eed9aa

File tree

5 files changed

+21
-2083
lines changed

5 files changed

+21
-2083
lines changed

Sources/FoundationEssentials/JSON/JSONScanner.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ internal struct JSONScanner {
265265
var reader: DocumentReader
266266
var depth: Int = 0
267267
var partialMap = JSONPartialMapData()
268-
private static let maximumRecursionDepth = 512
269268

270269
internal struct Options {
271270
var assumesTopLevelDictionary = false
@@ -413,7 +412,7 @@ internal struct JSONScanner {
413412
mutating func scanArray() throws {
414413
let firstChar = reader.read()
415414
precondition(firstChar == ._openbracket)
416-
guard self.depth < Self.maximumRecursionDepth else {
415+
guard self.depth < 512 else {
417416
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: 1))
418417
}
419418
self.depth &+= 1
@@ -471,7 +470,7 @@ internal struct JSONScanner {
471470
mutating func scanObject() throws {
472471
let firstChar = self.reader.read()
473472
precondition(firstChar == ._openbrace)
474-
guard self.depth < Self.maximumRecursionDepth else {
473+
guard self.depth < 512 else {
475474
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: -1))
476475
}
477476
try scanObject(withoutBraces: false)

Sources/FoundationEssentials/JSON/JSONWriter.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ internal struct JSONWriter {
2929
}
3030

3131
mutating func serializeJSON(_ value: JSONEncoderValue, depth: Int = 0) throws {
32-
guard depth < Self.maximumRecursionDepth else {
33-
throw JSONError.tooManyNestedArraysOrDictionaries()
34-
}
3532
switch value {
3633
case .string(let str):
3734
serializeString(str)
@@ -175,6 +172,10 @@ internal struct JSONWriter {
175172
}
176173

177174
mutating func serializeArray(_ array: [JSONEncoderValue], depth: Int) throws {
175+
guard depth < Self.maximumRecursionDepth else {
176+
throw JSONError.tooManyNestedArraysOrDictionaries()
177+
}
178+
178179
writer(ascii: ._openbracket)
179180
if pretty {
180181
writer(ascii: ._newline)
@@ -203,6 +204,10 @@ internal struct JSONWriter {
203204
}
204205

205206
mutating func serializePreformattedByteArray(_ bytes: [UInt8], _ lengths: [Int], depth: Int) throws {
207+
guard depth < Self.maximumRecursionDepth else {
208+
throw JSONError.tooManyNestedArraysOrDictionaries()
209+
}
210+
206211
writer(ascii: ._openbracket)
207212
if pretty {
208213
writer(ascii: ._newline)
@@ -237,6 +242,10 @@ internal struct JSONWriter {
237242
}
238243

239244
mutating func serializeObject(_ dict: [String:JSONEncoderValue], depth: Int) throws {
245+
guard depth < Self.maximumRecursionDepth else {
246+
throw JSONError.tooManyNestedArraysOrDictionaries()
247+
}
248+
240249
writer(ascii: ._openbrace)
241250
if pretty {
242251
writer(ascii: ._newline)

Tests/FoundationEssentialsTests/JSONEncoderTests.swift

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,19 +2492,13 @@ extension JSONEncoderTests {
24922492
prettyPrintEncoder.outputFormatting = .prettyPrinted
24932493

24942494
for encoder in [JSONEncoder(), prettyPrintEncoder] {
2495-
do {
2496-
let reencodedData = try encoder.encode(decoded)
2497-
let redecodedObjects = try decoder.decode(T.self, from: reencodedData)
2498-
XCTAssertEqual(decoded, redecodedObjects)
2499-
2500-
if let plistData {
2501-
let decodedPlistObjects = try PropertyListDecoder().decode(T.self, from: plistData)
2502-
XCTAssertEqual(decoded, decodedPlistObjects)
2503-
2504-
}
2505-
}
2506-
catch {
2507-
XCTFail("Pass test \"\(name) failed with error: \(error)")
2495+
let reencodedData = try! encoder.encode(decoded)
2496+
let redecodedObjects = try! decoder.decode(T.self, from: reencodedData)
2497+
XCTAssertEqual(decoded, redecodedObjects)
2498+
2499+
if let plistData {
2500+
let decodedPlistObjects = try! PropertyListDecoder().decode(T.self, from: plistData)
2501+
XCTAssertEqual(decoded, decodedPlistObjects)
25082502
}
25092503
}
25102504
}
@@ -2529,10 +2523,6 @@ extension JSONEncoderTests {
25292523
_run_passTest(name: "pass13", type: JSONPass.Test13.self)
25302524
_run_passTest(name: "pass14", type: JSONPass.Test14.self)
25312525
_run_passTest(name: "pass15", type: JSONPass.Test15.self)
2532-
// FIXME: Fix platform-specific crash on Windows, skipping test case for now
2533-
#if !os(Windows)
2534-
_run_passTest(name: "pass16", type: JSONPass.Test16.self)
2535-
#endif
25362526
}
25372527

25382528
func test_json5PassJSONFiles() {
@@ -2597,7 +2587,6 @@ extension JSONEncoderTests {
25972587
_run_failTest(name: "fail39", type: JSONFail.Test39.self)
25982588
_run_failTest(name: "fail40", type: JSONFail.Test40.self)
25992589
_run_failTest(name: "fail41", type: JSONFail.Test41.self)
2600-
_run_failTest(name: "fail42", type: JSONFail.Test42.self)
26012590

26022591
}
26032592

@@ -4379,12 +4368,6 @@ extension JSONPass {
43794368
}
43804369
}
43814370

4382-
extension JSONPass {
4383-
struct Test16: Codable, Equatable {
4384-
var nestedArray: [Test16]?
4385-
}
4386-
}
4387-
43884371
enum JSONFail {
43894372
typealias Test1 = String
43904373
typealias Test2 = [String]
@@ -4426,7 +4409,6 @@ enum JSONFail {
44264409
typealias Test39 = [String:String]
44274410
typealias Test40 = [String:String]
44284411
typealias Test41 = [String:String]
4429-
typealias Test42 = JSONPass.Test16
44304412
}
44314413

44324414
enum JSON5Pass { }

0 commit comments

Comments
 (0)