Skip to content

Commit 02956d7

Browse files
committed
[JSON] Do an experiment for deep recursion.
1 parent 99c5c0c commit 02956d7

File tree

291 files changed

+68
-51928
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

291 files changed

+68
-51928
lines changed

Package.swift

-24
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,6 @@ let package = Package(
175175
] + availabilityMacros + concurrencyChecking
176176
),
177177

178-
.testTarget(
179-
name: "FoundationInternationalizationTests",
180-
dependencies: [
181-
"TestSupport",
182-
"FoundationInternationalization",
183-
],
184-
swiftSettings: availabilityMacros + concurrencyChecking
185-
),
186-
187178
// FoundationMacros
188179
.macro(
189180
name: "FoundationMacros",
@@ -202,18 +193,3 @@ let package = Package(
202193
),
203194
]
204195
)
205-
206-
// https://github.com/apple/swift-package-manager/issues/7174
207-
// Test macro targets result in multiple definitions of `main` on Windows.
208-
#if !os(Windows)
209-
package.targets.append(contentsOf: [
210-
.testTarget(
211-
name: "FoundationMacrosTests",
212-
dependencies: [
213-
"FoundationMacros",
214-
"TestSupport"
215-
],
216-
swiftSettings: availabilityMacros + concurrencyChecking
217-
)
218-
])
219-
#endif

Sources/FoundationEssentials/JSON/JSONScanner.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ extension JSONMap.Value {
261261

262262

263263
internal struct JSONScanner {
264+
internal static let maximumRecursionDepth = JSONWriter.maximumRecursionDepth
265+
264266
let options: Options
265267
var reader: DocumentReader
266268
var depth: Int = 0
@@ -412,7 +414,7 @@ internal struct JSONScanner {
412414
mutating func scanArray() throws {
413415
let firstChar = reader.read()
414416
precondition(firstChar == ._openbracket)
415-
guard self.depth < 512 else {
417+
guard self.depth < Self.maximumRecursionDepth else {
416418
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: 1))
417419
}
418420
self.depth &+= 1
@@ -470,7 +472,7 @@ internal struct JSONScanner {
470472
mutating func scanObject() throws {
471473
let firstChar = self.reader.read()
472474
precondition(firstChar == ._openbrace)
473-
guard self.depth < 512 else {
475+
guard self.depth < Self.maximumRecursionDepth else {
474476
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: -1))
475477
}
476478
try scanObject(withoutBraces: false)

Sources/FoundationEssentials/JSON/JSONWriter.swift

+5-13
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 = 512
16+
internal static let maximumRecursionDepth = 1024
1717

1818
private var indent = 0
1919
private let pretty: Bool
@@ -29,6 +29,10 @@ 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+
}
35+
3236
switch value {
3337
case .string(let str):
3438
serializeString(str)
@@ -172,10 +176,6 @@ internal struct JSONWriter {
172176
}
173177

174178
mutating func serializeArray(_ array: [JSONEncoderValue], depth: Int) throws {
175-
guard depth < Self.maximumRecursionDepth else {
176-
throw JSONError.tooManyNestedArraysOrDictionaries()
177-
}
178-
179179
writer(ascii: ._openbracket)
180180
if pretty {
181181
writer(ascii: ._newline)
@@ -204,10 +204,6 @@ internal struct JSONWriter {
204204
}
205205

206206
mutating func serializePreformattedByteArray(_ bytes: [UInt8], _ lengths: [Int], depth: Int) throws {
207-
guard depth < Self.maximumRecursionDepth else {
208-
throw JSONError.tooManyNestedArraysOrDictionaries()
209-
}
210-
211207
writer(ascii: ._openbracket)
212208
if pretty {
213209
writer(ascii: ._newline)
@@ -242,10 +238,6 @@ internal struct JSONWriter {
242238
}
243239

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

Tests/FoundationEssentialsTests/AttributedString/AttributedStringCOWTests.swift

-260
This file was deleted.

0 commit comments

Comments
 (0)