JSONDecoder Limit

Hello,

I believe I have found a bug in the JSONDecoder. If one tries to decode a JSON object with more than 8 properties, I get an unrecognizable error: "error: Execution was interrupted, reason: EXCBADACCESS (code=1, address=0xe8).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation."
This error happens whenever one executes code similar to below:
Code Block
struct Objects: Codable {
  var NAME1: String?
  var NAME2: String?
  var NAME3: String?
  var NAME4: String?
  var NAME5: String?
  var NAME6: String?
  var NAME7: String?
  var NAME8: String?
  var NAME9: String?
}
let NEWDATA = """
[
{
  "NAME1": "hi",
  "NAME2": "hi",
  "NAME3": "hi",
  "NAME4": "hi",
  "NAME5": "hi",
  "NAME6": "hi",
  "NAME7": "hi",
  "NAME8": "hi",
  "NAME9": "HELLO"
}
]
""".data(using: .utf8)!
let decoder2 = JSONDecoder()
  decoder2.dataDecodingStrategy = .deferredToData
  decoder2.keyDecodingStrategy = .convertFromSnakeCase
  let product2 = try decoder2.decode([Objects].self, from: NEWDATA)
  print(product2)


This happens in both the Playground and on the device. Maybe I am doing something wrong but it works when you have only 8 properties in the Objects struct but not when you have 9 or more.

Xcode Version: 12.1
Swift Version: 5.3

Thanks for all your help,
  • A very frustrated developer

I tested in playground.

In Xcode 11.3, works OK

In Xcode 12GM, fails with 9 properties, but works if I remove any.

So same observation as yours.

I tested with 12.2 beta:
It works.

So it seems the bug has been corrected.
Please check on your side.

Nevertheless, that should be worth a bug report. Because it is a very serious regression from 11 to 12, and it is important to make sure there are no linked regression remaining in compiler.

Note: don't get frustrated by such a thing, otherwise your developer's life will be nightmarish…

Just tested with Xcode 12.1.

Works OK with 9 properties.
Problem is apparently corrected.
I am using Xcode 12.3 beta on Catalina 10.15.4. and i see the same behaviour in Playground.

A list of 10 properties works fine, but a list of 11 will not run. and wont throw an error.
I got a similar error playing in Playground (Xcode 12.2) with the initial data completed to 12 keys.
Error message:
NEWDATA 183 bytes
Playground execution terminated: An error was thrown and was not caught:
▿ DecodingError
▿ dataCorrupted : Context
  • codingPath : 0 elements

  • debugDescription : "The given data was not valid JSON."

▿ underlyingError : Optional<Error>
  • some : Error Domain=NSCocoaErrorDomain Code=3840 "Badly formed object around character 161." UserInfo={NSDebugDescription=Badly formed object around character 161.}

I then realised that I forget the ending comma in NEWDATA after  
    "NAME9": "HELLO"
Adding it solved the problem.

So I tested with 18 keys and works as well.

Problem occurs with Xcode 12.4 (12D4e). Eight properties good, nine properties bad:

Code Block
import Foundation
public struct Foo: Codable {
public let a01: String
public let a02: String
public let a03: String
public let a04: String
public let a05: String
public let a06: String
public let a07: String
public let a08: String
public let a09: String
}
let foo = Foo(a01: "foo",
a02: "foo",
a03: "foo",
a04: "foo",
a05: "foo",
a06: "foo",
a07: "foo",
a08: "foo",
a09: "foo")
do {
let encoder = JSONEncoder()
let data = try encoder.encode(foo)
if let jsonString = String(data: data, encoding: .utf8) {
print(jsonString)
}
} catch {
print(error)
}

Exception as follows:
Code Block
CoderBug.xcplaygroundpage: error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.


Now believe this is unrelated to JSONDecoder. It is the struct initialisation with nine properties that causes the execution abort.

Same kind of problem with Xcode 12.5.1 (12E507). Two nested structures with more than 3 properties and 3 more properties is too much and will throw no error - most of the time. Erratic behavior but extremely disabling.

Effectively, just initialising Foo with 9 var crashes Playground. That seems to be a playground problem.

But works OK in an app.

I did file a bug report. FB9393489

I just tested in Xcode 13ß4. No more crash. Playground bug is apparently corrected.

Apple replied to FB9080056 suggesting reproduce on Xcode 13. Can confirm on beta 4 (13A5201i) as noted by @Claude31 that the defect appears to have been resolved.

JSONDecoder Limit
 
 
Q