Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,18 @@ extension String.Encoding: XCJSON.Codable {
if let stringRepresentation {
try stringRepresentation.encode(with: coder)
} else {
try Int(rawValue).encode(with: coder)
let value = try Int(exactly: rawValue).unwrap(orThrow: "Unsupported text encoding \(rawValue)")
try value.encode(with: coder)
}
}

package init(with coder: XCJSON.Decoder) throws {
if coder.currentNodeType == .string {
self = try StringRepresentation(with: coder).memoryRepresentation
} else {
self = try Self(rawValue: UInt(Int(with: coder)))
let value = try Int(with: coder)
let rawValue = try UInt(exactly: value).unwrap(orThrow: "Invalid text encoding \(value)")
self = Self(rawValue: rawValue)
}
}
}
29 changes: 29 additions & 0 deletions Sources/Tests/TextEncodingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
// Copyright © 2026 Apple Inc. and the xcode-project-format project authors
//
// Licensed under Apache License v2.0
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//


import XcodeProjectFormat
import Foundation
import Testing

struct TextEncodingTests {
@Test func decodingAnOutOfRangeIntegerThrows() {
#expect(throws: NSError.self) {
let _: XCSchema.TextEncoding = try XCJSON.Decoder.decode(data: Data("-1".utf8))
}
#expect(throws: NSError.self) {
let _: XCSchema.TextEncoding = try XCJSON.Decoder.decode(data: Data("9223372036854775808".utf8))

@deccylol deccylol Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int and UInt bit-widths are platform-dependent. 9223372036854775808 is the 64-bit signed max value. It would be better to derive this number from the Int.min/max bounds. Int() wraps this to 0x8000000000000000 which is Int.min :) Looks like this second #expect is just testing the same behavior as the prior.

}
}

@Test func encodingAnOutOfRangeRawValueThrows() {
#expect(throws: NSError.self) {
_ = try XCJSON.Encoder.data(for: XCSchema.TextEncoding(rawValue: String.Encoding(rawValue: UInt.max)), options: .defaultOptions)
}
}
}
Loading