Skip to content

Fix #1176 JSON Decoder and Encoder limit disagreement #1242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/FoundationEssentials/JSON/JSONScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ internal struct JSONScanner {
var reader: DocumentReader
var depth: Int = 0
var partialMap = JSONPartialMapData()
private static let maximumRecursionDepth = 512

internal struct Options {
var assumesTopLevelDictionary = false
Expand Down Expand Up @@ -412,7 +413,7 @@ internal struct JSONScanner {
mutating func scanArray() throws {
let firstChar = reader.read()
precondition(firstChar == ._openbracket)
guard self.depth < 512 else {
guard self.depth < Self.maximumRecursionDepth else {
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: 1))
}
self.depth &+= 1
Expand Down Expand Up @@ -470,7 +471,7 @@ internal struct JSONScanner {
mutating func scanObject() throws {
let firstChar = self.reader.read()
precondition(firstChar == ._openbrace)
guard self.depth < 512 else {
guard self.depth < Self.maximumRecursionDepth else {
throw JSONError.tooManyNestedArraysOrDictionaries(location: reader.sourceLocation(atOffset: -1))
}
try scanObject(withoutBraces: false)
Expand Down
15 changes: 3 additions & 12 deletions Sources/FoundationEssentials/JSON/JSONWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ internal struct JSONWriter {
}

mutating func serializeJSON(_ value: JSONEncoderValue, depth: Int = 0) throws {
guard depth < Self.maximumRecursionDepth else {
throw JSONError.tooManyNestedArraysOrDictionaries()
}
switch value {
case .string(let str):
serializeString(str)
Expand Down Expand Up @@ -172,10 +175,6 @@ internal struct JSONWriter {
}

mutating func serializeArray(_ array: [JSONEncoderValue], depth: Int) throws {
guard depth < Self.maximumRecursionDepth else {
throw JSONError.tooManyNestedArraysOrDictionaries()
}

writer(ascii: ._openbracket)
if pretty {
writer(ascii: ._newline)
Expand Down Expand Up @@ -204,10 +203,6 @@ internal struct JSONWriter {
}

mutating func serializePreformattedByteArray(_ bytes: [UInt8], _ lengths: [Int], depth: Int) throws {
guard depth < Self.maximumRecursionDepth else {
throw JSONError.tooManyNestedArraysOrDictionaries()
}

writer(ascii: ._openbracket)
if pretty {
writer(ascii: ._newline)
Expand Down Expand Up @@ -242,10 +237,6 @@ internal struct JSONWriter {
}

mutating func serializeObject(_ dict: [String:JSONEncoderValue], depth: Int) throws {
guard depth < Self.maximumRecursionDepth else {
throw JSONError.tooManyNestedArraysOrDictionaries()
}

writer(ascii: ._openbrace)
if pretty {
writer(ascii: ._newline)
Expand Down
29 changes: 22 additions & 7 deletions Tests/FoundationEssentialsTests/JSONEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2492,13 +2492,19 @@ extension JSONEncoderTests {
prettyPrintEncoder.outputFormatting = .prettyPrinted

for encoder in [JSONEncoder(), prettyPrintEncoder] {
let reencodedData = try! encoder.encode(decoded)
let redecodedObjects = try! decoder.decode(T.self, from: reencodedData)
XCTAssertEqual(decoded, redecodedObjects)

if let plistData {
let decodedPlistObjects = try! PropertyListDecoder().decode(T.self, from: plistData)
XCTAssertEqual(decoded, decodedPlistObjects)
do {
let reencodedData = try encoder.encode(decoded)
let redecodedObjects = try decoder.decode(T.self, from: reencodedData)
XCTAssertEqual(decoded, redecodedObjects)

if let plistData {
let decodedPlistObjects = try PropertyListDecoder().decode(T.self, from: plistData)
XCTAssertEqual(decoded, decodedPlistObjects)

}
}
catch {
XCTFail("Pass test \"\(name) failed with error: \(error)")
}
}
}
Expand All @@ -2523,6 +2529,7 @@ extension JSONEncoderTests {
_run_passTest(name: "pass13", type: JSONPass.Test13.self)
_run_passTest(name: "pass14", type: JSONPass.Test14.self)
_run_passTest(name: "pass15", type: JSONPass.Test15.self)
_run_passTest(name: "pass16", type: JSONPass.Test16.self)
}

func test_json5PassJSONFiles() {
Expand Down Expand Up @@ -2587,6 +2594,7 @@ extension JSONEncoderTests {
_run_failTest(name: "fail39", type: JSONFail.Test39.self)
_run_failTest(name: "fail40", type: JSONFail.Test40.self)
_run_failTest(name: "fail41", type: JSONFail.Test41.self)
_run_failTest(name: "fail42", type: JSONFail.Test42.self)

}

Expand Down Expand Up @@ -4368,6 +4376,12 @@ extension JSONPass {
}
}

extension JSONPass {
struct Test16: Codable, Equatable {
var nestedArray: [Test16]?
}
}

enum JSONFail {
typealias Test1 = String
typealias Test2 = [String]
Expand Down Expand Up @@ -4409,6 +4423,7 @@ enum JSONFail {
typealias Test39 = [String:String]
typealias Test40 = [String:String]
typealias Test41 = [String:String]
typealias Test42 = JSONPass.Test16
}

enum JSON5Pass { }
Expand Down
Loading