Skip to content

rdar://133742708 (JSONDecoder crashes whole app on invalid input instead of throwing Error) #1043

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

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
16 changes: 8 additions & 8 deletions Sources/FoundationEssentials/JSON/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,8 @@ extension JSONDecoderImpl: Decoder {

var iter = jsonMap.makeObjectIterator(from: region.startOffset)
while let (keyValue, value) = iter.next() {
// Failing to unwrap a string here is impossible, as scanning already guarantees that dictionary keys are strings.
let key = try! self.unwrapString(from: keyValue, for: dictCodingPathNode, _CodingKey?.none)
// We know these values are keys, but UTF-8 decoding could still fail.
let key = try self.unwrapString(from: keyValue, for: dictCodingPathNode, _CodingKey?.none)
let value = try self.unwrap(value, as: dictType.elementType, for: dictCodingPathNode, _CodingKey(stringValue: key)!)
result[key]._setIfNil(to: value)
}
Expand Down Expand Up @@ -1202,14 +1202,14 @@ extension JSONDecoderImpl {
switch keyDecodingStrategy {
case .useDefaultKeys:
while let (keyValue, value) = iter.next() {
// Failing to unwrap a string here is impossible, as scanning already guarantees that dictionary keys are strings.
let key = try! impl.unwrapString(from: keyValue, for: codingPathNode, _CodingKey?.none)
// We know these values are keys, but UTF-8 decoding could still fail.
let key = try impl.unwrapString(from: keyValue, for: codingPathNode, _CodingKey?.none)
result[key]._setIfNil(to: value)
}
case .convertFromSnakeCase:
while let (keyValue, value) = iter.next() {
// Failing to unwrap a string here is impossible, as scanning already guarantees that dictionary keys are strings.
let key = try! impl.unwrapString(from: keyValue, for: codingPathNode, _CodingKey?.none)
// We know these values are keys, but UTF-8 decoding could still fail.
let key = try impl.unwrapString(from: keyValue, for: codingPathNode, _CodingKey?.none)

// Convert the snake case keys in the container to camel case.
// If we hit a duplicate key after conversion, then we'll use the first one we saw.
Expand All @@ -1219,8 +1219,8 @@ extension JSONDecoderImpl {
case .custom(let converter):
let codingPathForCustomConverter = codingPathNode.path
while let (keyValue, value) = iter.next() {
// Failing to unwrap a string here is impossible, as scanning already guarantees that dictionary keys are strings.
let key = try! impl.unwrapString(from: keyValue, for: codingPathNode, _CodingKey?.none)
// We know these values are keys, but UTF-8 decoding could still fail.
let key = try impl.unwrapString(from: keyValue, for: codingPathNode, _CodingKey?.none)

var pathForKey = codingPathForCustomConverter
pathForKey.append(_CodingKey(stringValue: key)!)
Expand Down
10 changes: 10 additions & 0 deletions Tests/FoundationEssentialsTests/JSONEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,16 @@ final class JSONEncoderTests : XCTestCase {

XCTAssertThrowsError(try decoder.decode(String.self, from: utf8_BOM + json.data(using: String._Encoding.utf16BigEndian)!))
}

func test_invalidKeyUTF8() {
// {"key[255]":"value"}
// The invalid UTF-8 byte sequence in the key should trigger a thrown error, not a crash.
let data = Data([123, 34, 107, 101, 121, 255, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125])
struct Example: Decodable {
let key: String
}
XCTAssertThrowsError(try JSONDecoder().decode(Example.self, from: data))
}

func test_valueNotFoundError() {
struct ValueNotFound : Decodable {
Expand Down