Skip to content

Commit a93d5dc

Browse files
committed
Move checking condition & add expect fail test
1 parent cf3c776 commit a93d5dc

File tree

4 files changed

+1036
-15
lines changed

4 files changed

+1036
-15
lines changed

Sources/FoundationEssentials/JSON/JSONScanner.swift

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

269270
internal struct Options {
270271
var assumesTopLevelDictionary = false
@@ -412,7 +413,7 @@ internal struct JSONScanner {
412413
mutating func scanArray() throws {
413414
let firstChar = reader.read()
414415
precondition(firstChar == ._openbracket)
415-
guard self.depth < 512 else {
416+
guard self.depth < Self.maximumRecursionDepth else {
416417
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: 1))
417418
}
418419
self.depth &+= 1
@@ -470,7 +471,7 @@ internal struct JSONScanner {
470471
mutating func scanObject() throws {
471472
let firstChar = self.reader.read()
472473
precondition(firstChar == ._openbrace)
473-
guard self.depth < 512 else {
474+
guard self.depth < Self.maximumRecursionDepth else {
474475
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: -1))
475476
}
476477
try scanObject(withoutBraces: false)

Sources/FoundationEssentials/JSON/JSONWriter.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
internal struct JSONWriter {
1414

1515
// Structures with container nesting deeper than this limit are not valid.
16-
private static let maximumRecursionDepth = 513
16+
private static let maximumRecursionDepth = 512
1717

1818
private var indent = 0
1919
private let pretty: Bool
@@ -29,6 +29,9 @@ 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+
}
3235
switch value {
3336
case .string(let str):
3437
serializeString(str)
@@ -172,10 +175,6 @@ internal struct JSONWriter {
172175
}
173176

174177
mutating func serializeArray(_ array: [JSONEncoderValue], depth: Int) throws {
175-
guard depth < Self.maximumRecursionDepth else {
176-
throw JSONError.tooManyNestedArraysOrDictionaries()
177-
}
178-
179178
writer(ascii: ._openbracket)
180179
if pretty {
181180
writer(ascii: ._newline)
@@ -204,10 +203,6 @@ internal struct JSONWriter {
204203
}
205204

206205
mutating func serializePreformattedByteArray(_ bytes: [UInt8], _ lengths: [Int], depth: Int) throws {
207-
guard depth < Self.maximumRecursionDepth else {
208-
throw JSONError.tooManyNestedArraysOrDictionaries()
209-
}
210-
211206
writer(ascii: ._openbracket)
212207
if pretty {
213208
writer(ascii: ._newline)
@@ -242,10 +237,6 @@ internal struct JSONWriter {
242237
}
243238

244239
mutating func serializeObject(_ dict: [String:JSONEncoderValue], depth: Int) throws {
245-
guard depth < Self.maximumRecursionDepth else {
246-
throw JSONError.tooManyNestedArraysOrDictionaries()
247-
}
248-
249240
writer(ascii: ._openbrace)
250241
if pretty {
251242
writer(ascii: ._newline)

Tests/FoundationEssentialsTests/JSONEncoderTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,7 @@ extension JSONEncoderTests {
25942594
_run_failTest(name: "fail39", type: JSONFail.Test39.self)
25952595
_run_failTest(name: "fail40", type: JSONFail.Test40.self)
25962596
_run_failTest(name: "fail41", type: JSONFail.Test41.self)
2597+
_run_failTest(name: "fail42", type: JSONFail.Test42.self)
25972598

25982599
}
25992600

@@ -4422,6 +4423,7 @@ enum JSONFail {
44224423
typealias Test39 = [String:String]
44234424
typealias Test40 = [String:String]
44244425
typealias Test41 = [String:String]
4426+
typealias Test42 = JSONPass.Test16
44254427
}
44264428

44274429
enum JSON5Pass { }

0 commit comments

Comments
 (0)