Skip to content

Commit ade1954

Browse files
authored
Fix a crash if an empty JSON configuration file is specified. (#612)
If you pass an empty file to `swift test --configuration-path`, we crash on an unexpected `nil` value. Fix it fix it fix it! ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 264fbd9 commit ade1954

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Sources/Testing/Support/JSON.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ enum JSON {
6262
static func decode<T>(_ type: T.Type, from jsonRepresentation: UnsafeRawBufferPointer) throws -> T where T: Decodable {
6363
#if canImport(Foundation)
6464
try withExtendedLifetime(jsonRepresentation) {
65-
let data = Data(
66-
bytesNoCopy: .init(mutating: jsonRepresentation.baseAddress!),
67-
count: jsonRepresentation.count,
68-
deallocator: .none
69-
)
65+
let byteCount = jsonRepresentation.count
66+
let data = if byteCount > 0 {
67+
Data(
68+
bytesNoCopy: .init(mutating: jsonRepresentation.baseAddress!),
69+
count: byteCount,
70+
deallocator: .none
71+
)
72+
} else {
73+
Data()
74+
}
7075
return try JSONDecoder().decode(type, from: data)
7176
}
7277
#else

Tests/TestingTests/ABIEntryPointTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#if canImport(Foundation) && !SWT_NO_ABI_ENTRY_POINT
1212
@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
1313

14+
#if canImport(Foundation)
15+
private import Foundation
16+
#endif
1417
private import _TestingInternals
1518

1619
@Suite("ABI entry point tests")
@@ -150,5 +153,14 @@ struct ABIEntryPointTests {
150153
// Call the entry point function.
151154
return try await abiEntryPoint(.init(argumentsJSON), recordHandler)
152155
}
156+
157+
#if canImport(Foundation)
158+
@Test func decodeEmptyConfiguration() throws {
159+
let emptyBuffer = UnsafeRawBufferPointer(start: nil, count: 0)
160+
#expect(throws: DecodingError.self) {
161+
_ = try JSON.decode(__CommandLineArguments_v0.self, from: emptyBuffer)
162+
}
163+
}
164+
#endif
153165
}
154166
#endif

0 commit comments

Comments
 (0)